perm filename LSPDOC.XGP[206,LSP]3 blob
sn#260926 filedate 1977-02-01 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BAXL30/FONT#1=BAXM30/FONT#2=BAXB30/FONT#3=SUB/FONT#4=SUP/FONT#5=BASL35/FONT#6=NGR25/FONT#7=MATH30/FONT#8=FIX25/FONT#9=GRKB30/FONT#11=GRFX25/FONT#12=GRFX35
␈↓ ↓H␈↓␈↓ εH␈↓ 91
␈↓ ↓H␈↓α␈↓ ∧/RECURSIVE PROGRAMMING IN LISP
␈↓ ↓H␈↓α␈↓ ¬Jby John McCarthy
␈↓ ↓H␈↓α␈↓ ¬AStanford University
␈↓ ↓H␈↓α␈↓ ∧`Copyright 1977 by John McCarthy
␈↓ ↓H␈↓␈↓ ∧πThis version printed at 6:05 on February 1, 1977.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i
␈↓ ↓H␈↓␈↓ ∧H␈↓&T A B L E O F C O N T E N T S␈↓)αβ
␈↓ ↓H␈↓SECTION␈↓
pPAGE
␈↓ ↓H␈↓I␈↓ α8INTRODUCTION TO LISP
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Lists.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ +_3
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Atoms.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ +_4
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(List structures.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ +_5
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(S-expressions.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ +_6
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(The basic functions and predicates of LISP.␈↓ λx. . . ␈↓ h␈↓ +_8
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Conditional expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ ≤_11
␈↓ ↓H␈↓␈↓ βλ7␈↓ ∧(Boolean expressions.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ ≤_12
␈↓ ↓H␈↓␈↓ βλ8␈↓ ∧(Recursive function definitions.␈↓ π8. . . . . . . . ␈↓ h␈↓ ≤_13
␈↓ ↓H␈↓␈↓ βλ9␈↓ ∧(Lambda expressions and functions with functions as
␈↓ ↓H␈↓␈↓ ¬(arguments.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ ≤_19
␈↓ ↓H␈↓␈↓ βλ10␈↓ ∧(Label.␈↓ ¬_. . . . . . . . . . . . . . . . ␈↓ h␈↓ ≤_22
␈↓ ↓H␈↓␈↓ βλ11␈↓ ∧(Numerical computation.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ ≤_22
␈↓ ↓H␈↓II␈↓ α8HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Static and dynamic ways of programming.␈↓ λH. . . . ␈↓ h␈↓ ≤_25
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Simple list recursion.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ ≤_26
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Simple S-expression recursion.␈↓ π8. . . . . . . . ␈↓ h␈↓ ≤_28
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Other structural recursions.␈↓ πλ. . . . . . . . . ␈↓ h␈↓ ≤_29
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(Tree search recursion.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ ≤_30
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Game trees.␈↓ ¬H. . . . . . . . . . . . . . . ␈↓ h␈↓ ≤_33
␈↓ ↓H␈↓␈↓ ¬_TABLE OF CONTENTS␈↓ 6ii
␈↓ ↓H␈↓III␈↓ α8COMPILING IN LISP
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(Introduction␈↓ ¬H. . . . . . . . . . . . . . . ␈↓ h␈↓ ≤_37
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Some facts about the PDP-10.␈↓ π8. . . . . . . . ␈↓ h␈↓ ≤_38
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Code produced by LISP compilers.␈↓ πh. . . . . . . ␈↓ h␈↓ ≤_39
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Listings of LCOM0 and LCOM4␈↓ πh. . . . . . . ␈↓ h␈↓ ≤_42
␈↓ ↓H␈↓IV␈↓ α8COMPUTABILITY
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(The function ␈↓↓eval␈↓.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ ≤_65
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Computability.␈↓ ¬x. . . . . . . . . . . . . ␈↓ h␈↓ ≤_68
␈↓ ↓H␈↓V␈↓ α8PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓␈↓ βλ1␈↓ ∧(First order logic with conditional forms and lambda-
␈↓ ↓H␈↓␈↓ ¬(expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ ≤_71
␈↓ ↓H␈↓␈↓ βλ2␈↓ ∧(Conditional forms.␈↓ ε(. . . . . . . . . . . . ␈↓ h␈↓ ≤_73
␈↓ ↓H␈↓␈↓ βλ3␈↓ ∧(Lambda-expressions.␈↓ εX. . . . . . . . . . . ␈↓ h␈↓ ≤_74
␈↓ ↓H␈↓␈↓ βλ4␈↓ ∧(Algebraic axioms for S-expressions and lists.␈↓ λx. . . ␈↓ h␈↓ ≤_75
␈↓ ↓H␈↓␈↓ βλ5␈↓ ∧(Axiom schemas of induction.␈↓ π8. . . . . . . . ␈↓ h␈↓ ≤_76
␈↓ ↓H␈↓␈↓ βλ6␈↓ ∧(Proofs by structural induction.␈↓ π8. . . . . . . . ␈↓ h␈↓ ≤_77
␈↓ ↓H␈↓␈↓ βλ7␈↓ ∧(First Order Theory of Partial Functions␈↓ λH. . . . ␈↓ h␈↓ ≤_78
␈↓ ↓H␈↓␈↓ εH␈↓ 93
␈↓ ↓H␈↓α␈↓ επChapter I
␈↓ ↓H␈↓α␈↓ ¬εINTRODUCTION TO LISP
␈↓ ↓H␈↓1. ␈↓αLists.␈↓
␈↓ ↓H␈↓␈↓ α_Symbolic␈αinformation␈αin␈αLISP␈αis␈α expressed␈α by␈α S-expressions␈αand␈α these␈α are␈α represented
␈↓ ↓H␈↓in␈α∞ the␈α
memory␈α∞of␈α
the␈α∞computer␈α∞by␈α
list␈α∞structures.␈α
Before␈α∞giving␈α
formal␈α∞ definitions,␈α∞ we␈α
shall
␈↓ ↓H␈↓give some examples.
␈↓ ↓H␈↓␈↓ α_The most common form of S-expression is the list, and here are some lists:
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(A B C E)
␈↓ ↓H␈↓has four elements.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(A B (C D) E)
␈↓ ↓H␈↓has four elements one of which is itself a list.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(A)
␈↓ ↓H␈↓has one element.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_((A B C D))
␈↓ ↓H␈↓also has one element which itself is a list.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_()
␈↓ ↓H␈↓has no elements; it is also written
␈↓ ↓H␈↓␈↓ α_NIL.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(PLUS X Y)
␈↓ ↓H␈↓has three elements and may be used to represent the expression
␈↓ ↓H␈↓␈↓ α_␈↓↓x + y.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(PLUS (TIMES X Y) X 3)
␈↓ ↓H␈↓has four elements and may be used to represent the expression
␈↓ ↓H␈↓␈↓ α_␈↓↓xy + x + 3.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(EXIST X (ALL Y (IMPLIES (P X) (P Y))))
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 94
␈↓ ↓H␈↓may be used to represent the logical expression
␈↓ ↓H␈↓␈↓ α_(∃␈↓↓x)(∀y).P(x)⊃P(y).
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_(INTEGRAL 0 ∞ (TIMES (EXP (TIMES I X Y)) (F X)) X)
␈↓ ↓H␈↓may be used to represent the expression
␈↓ ↓H␈↓␈↓ α_␈↓ ␈↓∧1␈↓ ␈↓∧0␈↓¬␈↓#
∞␈↓#␈↓↓e␈↓¬ixy␈↓↓f(x)dx.
␈↓ ↓H␈↓The list
␈↓ ↓H␈↓␈↓ α_((A B) (B A C D) (C B D E) (D B C E) (E C D F) (F E))
␈↓ ↓H␈↓is␈αused␈αto␈αrepresent␈αthe␈αnetwork␈αof␈αFigure␈α1␈αaccording␈α to␈α a␈α scheme␈αwhereby␈α there␈α is␈αa␈αsublist
␈↓ ↓H␈↓for each vertex consisting of the vertex itself followed by the vertices to which it is connected.
␈↓ ↓H␈↓ C
␈↓"π␈↓ ↓H␈↓ ≤'~`≥
␈↓ ↓H␈↓ ≤' ~ `≥
␈↓ ↓H␈↓ B ≤' ~ `≥ E
␈↓ ↓H␈↓␈↓ βX A αααα' ≤'␈↓ ↓H `≥ ≤'
␈↓"
␈↓ ↓H␈↓ D
␈↓ ↓H␈↓
␈↓ ↓H␈↓ Figure 1
␈↓ ↓H␈↓␈↓ α_The␈α
elements␈α
of␈α
a␈α
list␈α
are␈α∞surrounded␈α
by␈α
parentheses␈α
and␈α
separated␈α
by␈α
spaces.␈α∞ A␈α
list
␈↓ ↓H␈↓may␈α
have␈α
any␈α
number␈α
of␈αterms␈α
and␈α
any␈α
of␈α
these␈α
terms␈α may␈α
themselves␈α
be␈α
lists.␈α
In␈α this␈α
case,
␈↓ ↓H␈↓the␈αspaces␈αsurrounding␈αa␈αsublist␈α may␈α be␈α omitted,␈α and␈α extra␈α spaces␈α between␈αelements␈αof␈αa␈αlist
␈↓ ↓H␈↓are allowed. Thus the lists
␈↓ ↓H␈↓␈↓ α_(A B(C D) E)
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_(A B (C D) E)
␈↓ ↓H␈↓are regarded as the same.
␈↓ ↓H␈↓2. ␈↓αAtoms.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∞expressions␈α∞A,␈α∞B,␈α∞X,␈α∂Y,␈α∞3,␈α∞PLUS,␈α∞and␈α∞ALL␈α∂occurring␈α∞in␈α∞the␈α∞above␈α∞ lists␈α∂are␈α∞called
␈↓ ↓H␈↓atoms.␈α⊂ In␈α∂general,␈α⊂an␈α∂atom␈α⊂is␈α∂expressed␈α⊂by␈α∂a␈α⊂sequence␈α∂of␈α⊂capital␈α∂letters,␈α⊂ digits,␈α⊂ and␈α∂ special
␈↓ ↓H␈↓characters␈α
with␈αcertain␈α
exclusions.␈α
The␈αexclusions␈α
are␈α
<space>,␈α<carriage␈α
return>,␈α
and␈αthe␈α
other
␈↓ ↓H␈↓non-printing␈α↔ characters,␈α↔ and␈α⊗ also␈α↔ the␈α↔ parentheses,␈α⊗brackets,␈α↔ semi-colon,␈α↔ and␈α⊗ comma.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 95
␈↓ ↓H␈↓Numbers␈α∞are␈α∞expressed␈α∞as␈α∞signed␈α∞decimal␈α∞or␈α∞octal␈α∞numbers,␈α∞ the␈α∞ exact␈α∞ convention␈α
depending
␈↓ ↓H␈↓on␈α∞ the␈α
implementation.␈α∞ Floating␈α
point␈α∞ numbers␈α
are␈α∞ written␈α
with␈α∞decimal␈α
points␈α∞and,␈α
when
␈↓ ↓H␈↓appropriate,␈α⊃an␈α⊃exponent␈α∩notation␈α⊃depending␈α⊃ on␈α∩ the␈α⊃implementation.␈α⊃ The␈α∩ reader␈α⊃ should
␈↓ ↓H␈↓consult the programmer's manual for the LISP implementation he intends to use.
␈↓ ↓H␈↓␈↓ α_Some examples of atoms are
␈↓ ↓H␈↓␈↓ α_THE-LAST-TRUMP
␈↓ ↓H␈↓␈↓ α_A307B
␈↓ ↓H␈↓␈↓ α_345
␈↓ ↓H␈↓␈↓ α_3.14159,
␈↓ ↓H␈↓and from these we can form lists like
␈↓ ↓H␈↓␈↓ α_((345 3.14159 -47) A307B THE-LAST-TRUMP -45.21).
␈↓ ↓H␈↓3. ␈↓αList structures.␈↓
␈↓ ↓H␈↓␈↓ α_Lists␈α are␈α represented␈α
in␈αthe␈αmemory␈α
of␈αthe␈αcomputer␈α
by␈αlist␈αstructures.␈α
A␈αlist␈αstructure␈α
is
␈↓ ↓H␈↓a␈α∂collection␈α⊂of␈α∂memory␈α⊂words␈α∂ each␈α⊂of␈α∂ which␈α∂ is␈α⊂ divided␈α∂ into␈α⊂ two␈α∂ parts,␈α⊂and␈α∂each␈α⊂part␈α∂is
␈↓ ↓H␈↓capable␈αof␈α
containing␈αan␈αaddress␈α
in␈αmemory.␈αThe␈α
two␈αparts␈α
are␈αcalled␈αare␈α
called␈αthe␈α a-part␈α
and
␈↓ ↓H␈↓the␈α∞ d-part.␈α∞ There␈α∂ is␈α∞ one␈α∞computer␈α∂word␈α∞for␈α∞each␈α∞element␈α∂of␈α∞the␈α∞list,␈α∂and␈α∞the␈α∞a-part␈α∂of␈α∞the
␈↓ ↓H␈↓word␈α
contains␈α
the␈α
address␈α
of␈αthe␈α
list␈α
or␈α
atom␈α
representing␈αthe␈α
element,␈α
and␈α
the␈α
d-part␈αcontains
␈↓ ↓H␈↓the␈αaddress␈αof␈α
the␈αword␈αrepresenting␈αthe␈α
next␈αelement␈α of␈α
the␈α list.␈α If␈αthe␈α
list␈αelement␈αis␈α
itself␈αa
␈↓ ↓H␈↓list,␈α
then,␈α
of␈α
course,␈αthe␈α
address␈α
of␈α
the␈α
first␈αword␈α
of␈α
its␈α
list␈α
structure␈αis␈α
given␈α
in␈α
the␈α
a-part␈α of␈α
the
␈↓ ↓H␈↓word␈α∞ representing␈α∞ that␈α∞ element.␈α∞ A␈α∞diagram␈α∞shows␈α∞this␈α∞more␈α∞clearly␈α∞than␈α∞words,␈α∞and␈α∞the␈α∞list
␈↓ ↓H␈↓structure␈α∂corresponding␈α∞to␈α∂the␈α∞ list␈α∂ (PLUS␈α∞(TIMES␈α∂ X␈α∞ Y)␈α∂ X␈α∞ 3)␈α∂ which␈α∞may␈α∂represent␈α∞the
␈↓ ↓H␈↓expression ␈↓↓xy + x + 3 is shown in figure 2.
␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓ ↓H␈↓ ααααα→~ ~ εαααα→~ ~ εαααα→~ ~ εαααα→~ ~ εααααααα⊃
␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ %απα∀ααα$ ~
␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~
␈↓ ↓H␈↓ PLUS ~ ~ 3 ~
␈↓ ↓H␈↓ ~ ⊂αααπααα⊃ ~ ⊂αααπααα⊃ ⊂αααπααα⊃ ~
␈↓ ↓H␈↓ %α→~ ~ εααβα→~ ~ εαααα→~ ~ ~ ~
␈↓ ↓H␈↓ %απα∀ααα$ ~ %απα∀ααα$ %απα∀απα$ ~
␈↓ ↓H␈↓ ↓ ~ ~ ↓ ~ ~
␈↓ ↓H␈↓ TIMES %απαα$ Y %ααπα$
␈↓ ↓H␈↓ ↓ ↓
␈↓ ↓H␈↓ X NIL
␈↓ ↓H␈↓ Figure 2.
␈↓ ↓H␈↓␈↓ α_Atoms␈α
are␈α
represented␈α
by␈α
the␈α
addresses␈α
of␈α
their␈α
property␈α
lists␈α
which␈α
are␈α
list␈αstructures␈α
of
␈↓ ↓H␈↓a␈αspecial␈αkind␈α depending␈α on␈α the␈αimplementation.␈α (In␈α some␈α implementations,␈α the␈α first␈α word
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 96
␈↓ ↓H␈↓of␈α∞a␈α∞property␈α∞list␈α∞is␈α∞in␈α∞a␈α∞special␈α∂area␈α∞of␈α∞memory,␈α∞in␈α∞others␈α∞the␈α∞first␈α∞word␈α∞is␈α∂ distinguished␈α∞ by
␈↓ ↓H␈↓sign,␈αin␈αstill␈αothers␈αit␈αhas␈αa␈αspecial␈αa-part.␈α For␈αbasic␈αLISP␈αprogramming,␈αit␈αis␈α enough␈α to␈α know
␈↓ ↓H␈↓that atoms are distinguishable from other list structures by a predicate called ␈↓αat␈↓.)
␈↓ ↓H␈↓␈↓ α_The␈α last␈α word␈α of␈α a␈αlist␈αcannot␈αhave␈αthe␈αaddress␈αof␈αa␈αnext␈αword␈αin␈αits␈αd-part␈αsince␈αthere
␈↓ ↓H␈↓isn't any next word, so it has the address of a special atom called NIL.
␈↓ ↓H␈↓␈↓ α_A␈α∀program␈α∃referrs␈α∀ to␈α∀a␈α∃list␈α∀by␈α∀the␈α∃address␈α∀of␈α∀its␈α∃first␈α∀element.␈α∀ According␈α∃to␈α∀this
␈↓ ↓H␈↓convention,␈α∞we␈α∞see␈α∞that␈α∞the␈α∞a-part␈α∞ of␈α∞ a␈α∂list␈α∞ word␈α∞ is␈α∞ the␈α∞ list␈α∞ element␈α∞ and␈α∞ the␈α∞d-part␈α∂is␈α∞a
␈↓ ↓H␈↓pointer␈α
to␈αa␈α
sublist␈αformed␈α
by␈α
deleting␈αthe␈α
first␈αelement.␈α
Thus␈α
the␈αfirst␈α
word␈αof␈α
the␈α list␈α
structure
␈↓ ↓H␈↓of␈α
figure␈α 2␈α
contains␈α a␈α
pointer␈αto␈α
the␈αlist␈α
structure␈αrepresenting␈α
the␈αatom␈α
PLUS,␈αwhile␈α
its␈αd-
␈↓ ↓H␈↓part␈α∪points␈α∪to␈α∪the␈α∪list␈α∩ ((TIMES␈α∪X␈α∪Y)␈α∪X␈α∪3).␈α∩ The␈α∪second␈α∪word␈α∪contains␈α∪the␈α∪list␈α∩structure
␈↓ ↓H␈↓representing␈α (TIMES␈αX␈αY)␈α in␈α its␈α a-part␈α and␈α the␈α list␈α structure␈αrepresenting␈α (X␈α3)␈α in␈αits␈αd-
␈↓ ↓H␈↓part.␈α The␈αlast␈α
word␈αpoints␈αto␈αthe␈α
atom␈α3␈α in␈α
its␈αa-part␈αand␈αhas␈α
a␈αpointer␈αto␈α
the␈αatom␈α NIL␈α in␈α
its
␈↓ ↓H␈↓d-part. This is consistent with the convention that NIL represents the null list.
␈↓ ↓H␈↓4. ␈↓αS-expressions.␈↓
␈↓ ↓H␈↓␈↓ α_When␈α∞we␈α∞examine␈α∞the␈α∞way␈α∞list␈α∞structures␈α∞ represent␈α∞ lists␈α∞ we␈α∞see␈α∞ a␈α∂ curious␈α∞ asymmetry.
␈↓ ↓H␈↓Namely,␈α the␈α a-part␈αof␈αa␈αlist␈αword␈αcan␈αcontain␈αan␈αatom␈αor␈αa␈αlist,␈αbut␈αthe␈αd-part␈αcan␈αcontain␈αonly
␈↓ ↓H␈↓a␈αlist␈α or␈αthe␈αspecial␈αatom␈α NIL.␈α This␈αrestriction␈αis␈αquite␈αunnatural␈αfrom␈αthe␈αcomputing␈αpoint␈αof
␈↓ ↓H␈↓view,␈α∞ and␈α∞ we␈α∞ shall␈α∞ allow␈α∞ arbitrary␈α∞ atoms␈α∞ to␈α∞inhabit␈α∞ the␈α∞ d-parts␈α∞ of␈α∞ words,␈α∞but␈α∞then␈α∞we
␈↓ ↓H␈↓must␈α∩generalize␈α∩the␈α∪way␈α∩list␈α∩structures␈α∪are␈α∩expressed␈α∩as␈α∩character␈α∪strings.␈α∩To␈α∩ do␈α∪ this,␈α∩ we
␈↓ ↓H␈↓introduce the notion of S-expression.
␈↓ ↓H␈↓␈↓ α_An␈α∩ S-expression␈α⊃is␈α∩either␈α⊃an␈α∩atom␈α∩or␈α⊃a␈α∩pair␈α⊃of␈α∩S-expressions␈α⊃separated␈α∩by␈α∩" . "␈α⊃and
␈↓ ↓H␈↓surrounded by parentheses. In BNF, we can write
␈↓ ↓H␈↓<S-expression> ::= <atom> | (<S-expression> . <S-expression>).
␈↓ ↓H␈↓Examples of S-expressions are
␈↓ ↓H␈↓␈↓ α_A
␈↓ ↓H␈↓␈↓ α_(A . B)
␈↓ ↓H␈↓␈↓ α_(A . (B . A))
␈↓ ↓H␈↓␈↓ α_(PLUS (X . (Y . NIL)))
␈↓ ↓H␈↓␈↓ α_(3 . 3.4)
␈↓ ↓H␈↓The␈α⊂spaces␈α⊂around␈α⊂the␈α⊂.␈α⊂may␈α⊂be␈α⊂ omitted␈α⊂ when␈α⊂ this␈α⊂ will␈α⊂ not␈α⊂ cause␈α⊂confusion.␈α⊂ The␈α⊂only
␈↓ ↓H␈↓possible␈α
confusion␈αis␈α
of␈αthe␈α
dot␈αseparator␈α
with␈α
a␈αdecimal␈α
point␈αin␈α
numbers.␈α Thus,␈α
in␈α
the␈αabove
␈↓ ↓H␈↓cases,␈α∃we␈α∃ may␈α∃ write␈α∀(A.B),␈α∃ (A.(B.A)),␈α∃ and␈α∃ (PLUS.(X.(Y.NIL))),␈α∀ but␈α∃if␈α∃we␈α∃wrote␈α∀(3.3.4)
␈↓ ↓H␈↓confusion would result.
␈↓ ↓H␈↓␈↓ α_In␈αthe␈α
memory␈αof␈α
a␈αcomputer,␈αan␈α
S-expression␈α is␈α
represented␈αby␈α the␈α
address␈αof␈α
a␈αword
␈↓ ↓H␈↓whose␈αa-part␈αcontains␈αthe␈αfirst␈αelement␈αof␈αthe␈αpair␈αand␈αwhose␈αd-part␈αcontains␈αthe␈αsecond␈αelement
␈↓ ↓H␈↓of␈α→ the␈α→ pair.␈α→ Thus,␈α→ the␈α→S-expressions␈α→ (A.B),␈α→ (A.(B.A)),␈α→and␈α→ (PLUS.(X.(Y.NIL)))␈α_are
␈↓ ↓H␈↓represented by the list structures of figure 3.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 97
␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓ ↓H␈↓ ~ ~ ~ ~ ~ εαααα→~ ~ ~
␈↓ ↓H␈↓ %απα∀απα$ %απα∀ααα$ %απα∀απα$
␈↓ ↓H␈↓ ↓ ↓ ~ ↓ ~
␈↓ ↓H␈↓ A B ~ B ~
␈↓ ↓H␈↓ ~ ~
␈↓ ↓H␈↓ ~ ~
␈↓ ↓H␈↓ %ααααααααπαααααααα$
␈↓ ↓H␈↓ ↓
␈↓ ↓H␈↓ A
␈↓ ↓H␈↓ ⊂αααπααα⊃ ⊂αααπααα⊃ ⊂αααπααα⊃
␈↓ ↓H␈↓ ~ ~ εαααα→~ ~ εαααα→~ ~ ~
␈↓ ↓H␈↓ %απα∀ααα$ %απα∀ααα$ %απα∀απα$
␈↓ ↓H␈↓ ↓ ↓ ↓ ↓
␈↓ ↓H␈↓ PLUS X Y NIL
␈↓ ↓H␈↓ Figure 3.
␈↓ ↓H␈↓␈↓ α_Note␈α⊃that␈α∩the␈α⊃list␈α⊃(PLUS␈α∩X␈α⊃Y)␈α⊃and␈α∩the␈α⊃S-expression␈α⊃ (PLUS␈α∩.␈α⊃(X␈α⊃.␈α∩(Y␈α⊃.␈α∩NIL)))␈α⊃ are
␈↓ ↓H␈↓represented␈α in␈α memory␈α by␈α the␈α same␈α list␈αstructure.␈α The␈αsimplest␈αway␈αto␈αtreat␈αthis␈αis␈αto␈αregard
␈↓ ↓H␈↓S-expressions␈α∂as␈α∞primary␈α∂and␈α∞lists␈α∂ as␈α∞ abbreviations␈α∂ for␈α∞ certain␈α∂ S-expressions,␈α∂namely␈α∞those
␈↓ ↓H␈↓that␈α∞never␈α∞have␈α∞any␈α∂atom␈α∞but␈α∞ NIL␈α∞ as␈α∂the␈α∞second␈α∞part␈α∞of␈α∞a␈α∂pair.␈α∞In␈α∞giving␈α∞ input␈α∂ to␈α∞ LISP,
␈↓ ↓H␈↓either␈α∞ the␈α∞ list␈α∞ form␈α∞ or␈α∞ the␈α∞S-expression␈α∞ form␈α
may␈α∞be␈α∞used␈α∞for␈α∞lists.␈α∞ On␈α∞output,␈α∞LISP␈α
will
␈↓ ↓H␈↓print␈α
a␈α
list␈α
structure␈α
as␈α
a␈α
list␈α
as␈α
far␈α
as␈α
it␈α
can,␈α
otherwise␈α
as␈α
an␈α
S-expression.␈α
Thus,␈α
some␈α
list
␈↓ ↓H␈↓structures will be printed in a mixed notation, e.g. ((A . B) (C . D) (3)).
␈↓ ↓H␈↓␈↓ α_In␈α
general,␈α
the␈α
list␈α
(␈↓↓a␈α
b␈α
.␈α
.␈α
.␈α
z)␈↓␈α
may␈α
be␈α
regarded␈α
as␈α
an␈α
abbreviation␈α
of␈α
the␈α
S-expression␈α
(␈↓↓a␈α
.
␈↓ ↓H␈↓↓(b . (c . (... (z . NIL) ...)))␈↓.
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓␈↓ α_1. If we represent sums and products as indicated above and
␈↓ ↓H␈↓use␈α (MINUS␈αX),␈α (QUOTIENT␈αX␈αY),␈αand␈α (POWER␈αX␈αY)␈α as␈αrepresentations␈αof␈α -␈↓↓x␈↓,␈α ␈↓↓x/y␈↓,␈αand
␈↓ ↓H␈↓␈↓↓x␈↓¬y␈↓ respectively, then
␈↓ ↓H␈↓␈↓ α_a. What do the lists
␈↓ ↓H␈↓␈↓ α_(QUOTIENT 2 (POWER (PLUS X (MINUS Y)) 3))
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_(PLUS -2 (MINUS 2) (TIMES X (POWER Y 3.3)))
␈↓ ↓H␈↓represent?
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 98
␈↓ ↓H␈↓␈↓ α_b. How are the expressions ␈↓↓xyz+3(u+v)␈↓¬-3␈↓ and (␈↓↓xy-yx)/(xy+yx)␈↓ to be represented?
␈↓ ↓H␈↓␈↓ α_2. In the above mentioned graph notation, what graph is represented by the list
␈↓ ↓H␈↓␈↓ α_((A D E F)(B D E F)(C D E F)(D A B C)(E A B C)(F A B C))?
␈↓ ↓H␈↓␈↓ α_3.␈α
Write␈α
the␈α
list␈α
((PLUS␈α
(TIMES␈α
X␈αY)␈α
X␈α
3)␈α
as␈α
an␈α
S-expression.␈α
This␈α
is␈αsometimes␈α
referred
␈↓ ↓H␈↓to as "dot-notation".
␈↓ ↓H␈↓␈↓ α_4. Write the following S-expressions in list notation to whatever extent is possible:
␈↓ ↓H␈↓␈↓ α_a. (A . NIL)
␈↓ ↓H␈↓␈↓ α_b. (A . B)
␈↓ ↓H␈↓␈↓ α_c. ((A . NIL) . B)
␈↓ ↓H␈↓␈↓ α_d. ((A . B) . ((C . D) . NIL).
␈↓ ↓H␈↓␈↓ α_5.␈αProve␈αthat␈αthe␈αnumber␈αof␈α"shapes"␈αof␈αS-expressions␈αwith␈α␈↓↓n␈↓␈αatoms␈αis␈α␈↓↓(2n␈α-␈α2)!/(n!(n␈α-␈α1)!)␈↓.
␈↓ ↓H␈↓(The two shapes of S-expressions with three atoms are (A.(B.C)) and ((A.B).C)).
␈↓ ↓H␈↓␈↓ α_6. The above result can also be obtained by writing ␈↓↓S =
␈↓ ↓H␈↓1A
␈↓ ↓H␈↓2+
␈↓ ↓H␈↓3S␈↓αx␈↓↓S␈↓␈α∂as␈α∂an␈α∞"equation"␈α∂satisfied␈α∂by␈α∞the␈α∂set␈α∂of␈α∞S-expressions␈α∂with␈α∂the␈α∞interpretation␈α∂that␈α∂an␈α∞S-
␈↓ ↓H␈↓expression␈αis␈αeither␈αan␈αatom␈αor␈αa␈αpair␈αof␈αS-expressions.␈α The␈αnext␈αstep␈αis␈αto␈αregard␈αthe␈αequation
␈↓ ↓H␈↓as␈α
the␈α
quadratic␈α∞␈↓↓S␈↓¬2␈↓↓ - S + A = 0␈↓,␈α
solve␈α
it␈α∞by␈α
the␈α
quadratic␈α
formula␈α∞choosing␈α
the␈α
minus␈α∞sign␈α
for
␈↓ ↓H␈↓the␈α
square␈α
root.␈α
Then␈α
the␈α∞radical␈α
is␈α
regarded␈α
as␈α
the␈α∞1/2␈α
power␈α
and␈α
expanded␈α
by␈α∞the␈α
binomial
␈↓ ↓H␈↓theorem.␈α
Manipulating␈αthe␈α
binomial␈α
coefficients␈αyields␈α
the␈αabove␈α
formula␈α
as␈αthe␈α
coefficient␈αof␈α
␈↓↓A␈↓¬n␈↓
␈↓ ↓H␈↓in␈αthe␈α
expansion.␈α Why␈αdoes␈α
this␈αsomewhat␈α
ill-motivated␈αand␈αirregular␈α
procedure␈αgive␈α
the␈αright
␈↓ ↓H␈↓answer?
␈↓ ↓H␈↓␈↓ α_The last two exercises are unconnected with subsequent material in these notes.
␈↓ ↓H␈↓5. ␈↓αThe basic functions and predicates of LISP.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∞main␈α∞form␈α
of␈α∞LISP␈α∞program␈α
is␈α∞the␈α∞LISP␈α
function,␈α∞and␈α∞LISP␈α
functions␈α∞are␈α∞built␈α
up
␈↓ ↓H␈↓from␈α
basic␈αLISP␈α
functions,␈αpredicates,␈α
variables␈αand␈α
constants.␈α An␈α
expression␈αwith␈α
variables␈αin␈α
it
␈↓ ↓H␈↓whose␈α∪value␈α∪depends␈α∪on␈α∪the␈α∪values␈α∪of␈α∩the␈α∪variables␈α∪is␈α∪called␈α∪a␈α∪␈↓↓form␈↓.␈α∪ LISP␈α∪functions␈α∩are
␈↓ ↓H␈↓constructed␈α⊂from␈α⊂LISP␈α⊃␈↓↓forms␈↓␈α⊂which␈α⊂are␈α⊃written␈α⊂in␈α⊂two␈α⊃languages␈α⊂-␈α⊂␈↓↓publication␈α⊃language␈↓␈α⊂and
␈↓ ↓H␈↓␈↓↓internal␈α∞language␈↓.␈α∞ Publication␈α∞language␈α∞is␈α∞easier␈α∞for␈α∞people␈α∞to␈α∞read␈α∞and␈α∞write,␈α∞but␈α
publication
␈↓ ↓H␈↓language␈αprograms␈αare␈αnot␈αS-expressions,␈αand␈αtherefore␈α
it␈αis␈αnot␈αeasy␈αto␈αwrite␈αLISP␈α
programs␈αto
␈↓ ↓H␈↓generate,␈α∩interpret␈α⊃or␈α∩compile␈α⊃other␈α∩LISP␈α⊃programs␈α∩when␈α⊃these␈α∩programs␈α⊃are␈α∩in␈α⊃publication
␈↓ ↓H␈↓language.␈α The␈α
internal␈αlanguage␈α
programs␈αare␈α
S-expressions␈αand␈α
are␈αharder␈α
for␈αa␈α
person␈αto␈α
read
␈↓ ↓H␈↓and␈αwrite,␈αbut␈αit␈αis␈αeasier␈α
for␈αa␈αperson␈αto␈αwrite␈αa␈α
program␈αto␈αmanipulate␈α␈↓↓object␈αprograms␈↓␈αwhen␈α
the
␈↓ ↓H␈↓object␈α
programs␈α
are␈α
in␈α
internal␈α
language.␈α Besides␈α
all␈α
that,␈α
most␈α
LISP␈α
implementations␈αaccept␈α
only
␈↓ ↓H␈↓internal language programs rather than some form of publication language.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ 99
␈↓ ↓H␈↓␈↓ α_Just␈α∞as␈α∞numerical␈α∞computer␈α∞programs␈α∞are␈α
based␈α∞ on␈α∞ the␈α∞ four␈α∞arithmetic␈α∞ operations␈α
of
␈↓ ↓H␈↓addition␈α∂subtraction,␈α∂multiplication,␈α∂and␈α∂division,␈α∂and␈α∂the␈α∂arthmetic␈α∂predicates␈α∂of␈α∂equality␈α∂and
␈↓ ↓H␈↓comparison,␈α∂so␈α∂symbolic␈α∂ computation␈α∞ has␈α∂ its␈α∂basic␈α∂predicates␈α∞and␈α∂functions.␈α∂ LISP␈α∂has␈α∞three
␈↓ ↓H␈↓basic␈αfunctions␈αand␈αtwo␈αpredicates.␈α Each␈αwill␈αbe␈αexplained␈αfirst␈αin␈αits␈αpublication␈αform,␈αand␈αthen
␈↓ ↓H␈↓the internal form will be given.
␈↓ ↓H␈↓␈↓ α_First,␈α
we␈α
have␈α
two␈αfunctions␈α
␈↓αa␈↓␈α
and␈α
␈↓αd␈↓.␈α
␈↓αa␈α␈↓↓x␈↓␈α
is␈α
the␈α
a-part␈α
of␈αthe␈α
S-expression␈α
x.␈α
Thus,␈α ␈↓αa(A␈α
.
␈↓ ↓H␈↓αB)␈α
=␈α
A␈↓,␈α
and␈α
␈↓αa((A␈α
.␈α
B)␈α
.␈α
A)␈α
=␈α
(A␈α
.␈α
B)␈↓.␈α
␈↓αd␈α
␈↓↓x␈↓␈α
is␈α
the␈α
d-part␈α
of␈α
the␈α
S-expression␈α
␈↓↓x␈↓.␈α
Thus␈α
␈↓αd(A␈α
.␈α
B)␈α
=␈αB␈↓,␈α
and
␈↓ ↓H␈↓␈↓αd((A␈α
.␈α
B)␈α
.␈α
A)␈α
=␈α
A␈↓.␈α
␈↓αa␈α␈↓↓x␈↓␈α
and␈α
␈↓αd␈α
␈↓↓x␈↓␈α
are␈α
undefined␈α
in␈α
basic␈αLISP␈α
when␈α
␈↓↓x␈↓␈α
is␈α
an␈α
atom,␈α
but␈α
they␈αmay
␈↓ ↓H␈↓have␈αa␈αmeaning␈αin␈αsome␈αimplementations.␈α The␈αinternal␈αforms␈αof␈α␈↓αa␈α␈↓↓x␈↓␈αand␈α␈↓αd␈α␈↓↓x␈↓␈αare␈α(CAR␈αX)␈αand
␈↓ ↓H␈↓(CDR␈α∂X)␈α∂respectively.␈α∂ The␈α⊂names␈α∂CAR␈α∂and␈α∂CDR␈α∂stood␈α⊂for␈α∂"contents␈α∂of␈α∂the␈α∂address␈α⊂part␈α∂of
␈↓ ↓H␈↓register"␈α
and␈α
"contents␈α
of␈α
the␈α
decrement␈α
part␈α∞of␈α
register"␈α
in␈α
a␈α
1957␈α
precursor␈α
of␈α∞LISP␈α
projected
␈↓ ↓H␈↓for the IBM 704 computer. The names have persisted for lack of a clearly preferable alternative.
␈↓ ↓H␈↓␈↓ α_A␈α⊂certain␈α⊂ambiguity␈α⊂arises␈α⊃when␈α⊂we␈α⊂want␈α⊂to␈α⊃use␈α⊂S-expression␈α⊂constants␈α⊂in␈α⊃the␈α⊂internal
␈↓ ↓H␈↓language.␈α∂ Namely,␈α∂the␈α∂S-expression␈α∂A␈α∂could␈α∂either␈α∂stand␈α∂for␈α∂itself␈α∂or␈α∂stand␈α∂for␈α∂a␈α∂variable␈α∂A
␈↓ ↓H␈↓whose␈αvalue␈αis␈αwanted.␈α Even␈α(CAR␈αX)␈αis␈αambiguous␈αsince␈αsometime␈αthe␈αS-expression␈α
(CAR␈αX)
␈↓ ↓H␈↓itself␈α∩must␈α∩be␈α⊃referred␈α∩to␈α∩and␈α⊃not␈α∩the␈α∩result␈α⊃of␈α∩evaluating␈α∩it.␈α⊃ Internal␈α∩language␈α∩avoids␈α⊃the
␈↓ ↓H␈↓ambiguity␈α
by␈α
using␈α∞(QUOTE␈α
␈↓↓e)␈↓␈α
to␈α∞stand␈α
for␈α
the␈α
S-exression␈α∞␈↓↓e␈↓.␈α
Thus␈α
the␈α∞publication␈α
language
␈↓ ↓H␈↓form␈α
␈↓αa␈α(A␈α
B)␈↓␈α
corresponds␈αto␈α
the␈αinternal␈α
language␈α
form␈α(CAR␈α
(QUOTE␈α(A.B)))␈α
and␈α
both␈αhave
␈↓ ↓H␈↓the value A obtained by taking the ␈↓αa␈↓-part of the S-expression (A.B).
␈↓ ↓H␈↓␈↓ α_Since␈α lists␈α are␈α a␈α
particular␈α kind␈α of␈α S-expression,␈αthe␈α
meanings␈αof␈α ␈↓αa␈↓␈α and␈α ␈↓αd␈↓␈α
for␈αlists
␈↓ ↓H␈↓are also determined by the above definitions. Thus, we have
␈↓ ↓H␈↓␈↓ α_␈↓αa(A B C D) = A
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓αd(A B C D) = (B C D),
␈↓ ↓H␈↓and␈α
we␈αsee␈α
that,␈αin␈α
general,␈α ␈↓αa␈α
␈↓↓x␈↓␈α is␈α
the␈αfirst␈α
element␈α
of␈α the␈α
list␈α ␈↓↓x␈↓,␈α
and␈α ␈↓αd␈α
␈↓↓x␈↓␈α is␈α
the␈αrest␈α
of␈αthe␈α
list,
␈↓ ↓H␈↓deleting that first element.
␈↓ ↓H␈↓␈↓ α_Notice␈α that␈α for␈αS-expressions,␈αthe␈αdefinitions␈αof␈α ␈↓αa␈α␈↓↓x␈↓␈α and␈α␈↓αd␈α␈↓↓x␈↓␈α are␈αsymmetrical,␈αwhile␈αfor
␈↓ ↓H␈↓lists␈α
the␈α
symmetry␈αis␈α
lost.␈α
This␈α
is␈αbecause␈α
of␈α
the␈α
unsymmetrical␈αnature␈α
of␈α
the␈α
convention␈αthat
␈↓ ↓H␈↓defines lists in terms of S-expressions.
␈↓ ↓H␈↓␈↓ α_Notice,␈αfurther,␈α our␈α convention␈α of␈α writing␈α ␈↓αa␈↓␈α and␈α ␈↓αd␈↓ ␈αwithout␈α brackets␈α surrounding
␈↓ ↓H␈↓the␈αargument.␈α Also,␈αwe␈αuse␈αlower␈αcase␈αletters␈αfor␈αour␈αfunction␈αnames␈αand␈αfor␈αvariables,␈αreserving
␈↓ ↓H␈↓the upper case letters for the S-expressions themselves.
␈↓ ↓H␈↓␈↓ α_The␈α∞ third␈α∞ function␈α∞ ␈↓↓cons[x,␈α∞y]␈↓␈α∞ forms␈α
the␈α∞S-expression␈α∞whose␈α∞a-part␈α∞and␈α∞d-part␈α∞are␈α
␈↓↓x␈↓
␈↓ ↓H␈↓and ␈↓↓y␈↓ respectively. Thus
␈↓ ↓H␈↓␈↓ α_␈↓↓cons[(A.B), A] = ((A.B).A).
␈↓ ↓H␈↓␈↓ α_We␈α∞ see␈α∞ that␈α∞ for␈α∂ lists,␈α∞ ␈↓↓cons␈↓␈α∞ is␈α∞a␈α∞prefixing␈α∂operation.␈α∞ Namely,␈α∞ ␈↓↓cons[x,␈α∞y]␈↓␈α∞ is␈α∂the␈α∞list
␈↓ ↓H␈↓obtained by putting the element ␈↓↓x␈↓ onto the front of the list ␈↓↓y␈↓. Thus
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *10
␈↓ ↓H␈↓␈↓ α_␈↓↓cons[A, (B C D E)] = (A B C D E).
␈↓ ↓H␈↓If␈αwe␈αwant␈α ␈↓↓cons[x,␈αy]␈↓␈α to␈α be␈α a␈α list,␈α then␈α ␈↓↓y␈↓␈α must␈α be␈α a␈α list␈α(possibly␈αthe␈αnull␈αlist␈α NIL),␈αand
␈↓ ↓H␈↓␈↓↓x␈↓␈α
must␈α
be␈α
a␈α
list␈α
or␈α
an␈α
atom.␈α
In␈α
the␈α
case␈α
of␈α
S-expressions␈α
in␈α
general,␈α
there␈α
is␈α
no␈α
restriction␈α
on␈α
the
␈↓ ↓H␈↓arguments of ␈↓↓cons␈↓. Usually, we shall write ␈↓↓x . y␈↓ instead of ␈↓↓cons[x, y]␈↓ since this is briefer.
␈↓ ↓H␈↓␈↓ α_The␈αinternal␈αlanguage␈αform␈αof␈α␈↓↓cons[x,y]␈↓␈αis␈α(CONS␈αX␈αY).␈α The␈αname␈α"CONS"␈αcomes␈αfrom
␈↓ ↓H␈↓"construct" referring to the way ␈↓↓cons[x,y]␈↓ is constructed from the free storage list.
␈↓ ↓H␈↓␈↓ α_The␈α first␈α predicate␈α is␈α ␈↓αat␈α␈↓↓x␈↓.␈α ␈↓αat␈α␈↓↓x␈↓␈α is␈α true␈α if␈α the␈αS-expression␈α ␈↓↓x␈↓␈α is␈αatomic␈αand␈αfalse
␈↓ ↓H␈↓otherwise. The internal form is (ATOM X).
␈↓ ↓H␈↓␈↓ α_The␈α∪ second␈α∪ predicate␈α∩ is␈α∪equality␈α∪of␈α∪atomic␈α∩symbols␈α∪written␈α∪␈↓↓x␈α∩␈↓αeq␈α∪␈↓↓y␈↓.␈α∪Equality␈α∪of␈α∩S-
␈↓ ↓H␈↓expressions␈α∞is␈α
tested␈α∞by␈α
a␈α∞ program␈α
based␈α∞ on␈α
␈↓αeq␈↓.␈α∞ Actually␈α
␈↓αeq␈↓␈α∞ does␈α
a␈α∞bit␈α
more␈α∞than␈α
testing
␈↓ ↓H␈↓equality␈α
of␈α
atoms.␈α
Namely,␈α
␈↓↓x␈α
␈↓αeq␈α
␈↓↓y␈↓␈α
is␈α
true␈α
if␈α
and␈α
only␈α
if␈α
the␈α
addresses␈α
of␈α
the␈α
first␈α
words␈α of
␈↓ ↓H␈↓the␈α S-expressions␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈αequal.␈α Therefore,␈αif␈α␈↓↓x␈α␈↓αeq␈α␈↓↓y␈↓,␈αthen␈α ␈↓↓x␈↓␈α and␈α␈↓↓y␈↓␈αare␈α
certainly␈α the
␈↓ ↓H␈↓same␈α S-expression␈α since␈αthey␈α are␈α represented␈αby␈αthe␈αsame␈αstructure␈αin␈αmemory.␈α The␈αconverse
␈↓ ↓H␈↓is␈α
false␈α
because␈αthere␈α
is␈α
no␈α
guarantee␈α in␈α
general␈α
that␈α
the␈α same␈α
S-expression␈α
is␈αnot␈α
represented
␈↓ ↓H␈↓by␈α
two␈α
different␈α
list␈α
structures.␈α
In␈α
the␈α
particular␈αcase␈α
where␈α
at␈α
least␈α
one␈α
of␈α
the␈α
S-expressions␈αis
␈↓ ↓H␈↓known␈α
to␈α be␈α
an␈α
atom,␈αthis␈α
guarantee␈αcan␈α
be␈α
given,␈αbecause␈α
LISP␈αrepresents␈α
atoms␈α
uniquely␈αin
␈↓ ↓H␈↓memory. The internal form is (EQ X Y).
␈↓ ↓H␈↓␈↓ α_The␈α∂above␈α∂are␈α∂all␈α⊂the␈α∂basic␈α∂functions␈α∂of␈α∂LISP;␈α⊂all␈α∂other␈α∂LISP␈α∂functions␈α∂ can␈α⊂ be␈α∂ built
␈↓ ↓H␈↓from␈α
them␈α
using␈α
recursive␈α
conditional␈α
expressions␈α
as␈α
will␈α
shortly␈α
be␈α
explained.␈α∞However,␈α
the
␈↓ ↓H␈↓use of certain abbreviations makes LISP programs easier to write and understand.
␈↓ ↓H␈↓␈↓ α_␈↓αn␈α␈↓↓x␈↓␈α is␈α
an␈α abbreviation␈αfor␈α
␈↓↓x␈α␈↓αeq␈αNIL␈↓.␈α It␈α
rates␈αa␈αspecial␈α
notation␈αbecause␈α NIL␈α plays␈α
the
␈↓ ↓H␈↓same␈α
role␈α
among␈α
lists␈α∞ that␈α
zero␈α
plays␈α
among␈α
numbers,␈α∞ and␈α
list␈α
variables␈α
often␈α
have␈α∞to␈α
be
␈↓ ↓H␈↓tested to see if their value is NIL. Its internal form is (NULL X).
␈↓ ↓H␈↓ The␈αnotation␈α␈↓↓list[x1,␈αx2,␈α.␈α.␈α.␈α,␈αxn]␈↓␈αis␈αused␈αto␈αdenote␈αthe␈αcomposition␈αof␈α␈↓↓cons␈↓'s␈αthat␈αforms␈αa
␈↓ ↓H␈↓list from its elements. Thus
␈↓ ↓H␈↓␈↓ α_␈↓↓list[x, y, z] = cons[x, cons[y, cons[z, NIL]]]␈↓
␈↓ ↓H␈↓and␈α
forms␈αa␈α
list␈α
of␈αthree␈α
elements␈αout␈α
of␈α
the␈αquantities␈α
␈↓↓x,␈α
y,␈α␈↓␈α
and␈α␈↓↓z␈↓.␈α
We␈α
often␈αwrite␈α
<␈↓↓x␈α
y␈α.␈α
.␈α.␈α
z>␈↓
␈↓ ↓H␈↓instead␈αof␈α␈↓↓list[x,␈αy,␈α.␈α .␈α.␈α,␈α
z]␈↓␈αwhen␈αthis␈αwill␈αnot␈αcause␈αconfusion.␈α The␈αexperienced␈α
implementer
␈↓ ↓H␈↓of␈α∂programming␈α∂languages␈α⊂will␈α∂expect␈α∂that␈α⊂since␈α∂␈↓↓list␈↓␈α∂has␈α∂a␈α⊂variable␈α∂number␈α∂of␈α⊂arguments,␈α∂its
␈↓ ↓H␈↓implementation␈αwill␈αpose␈αproblems.␈α He␈αwill␈αbe␈αright.␈α The␈αinternal␈αform␈αof␈α␈↓↓<x␈αy␈α...␈αz>␈↓␈αis␈α(LIST
␈↓ ↓H␈↓X Y ... Z).
␈↓ ↓H␈↓␈↓ α_Compositions␈αof␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓␈αare␈αwritten␈αby␈αconcatenating␈αthe␈αletters␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α Thus,␈αit␈αis␈αeasily
␈↓ ↓H␈↓seen␈α
that␈α
␈↓αad␈α
␈↓↓x␈↓␈α
denotes␈α
the␈αsecond␈α
element␈α
of␈α
the␈α
list␈α
␈↓↓x␈↓,␈α
and␈α␈↓αadd␈α
␈↓↓x␈↓␈α
denotes␈α
the␈α
third␈α
element␈αof␈α
that
␈↓ ↓H␈↓list. The internal forms of these functions are CADR and CADDR respectively.
␈↓ ↓H␈↓␈↓ α_Besides␈α
the␈α
basic␈α
functions␈αof␈α
LISP,␈α
there␈α
will␈αbe␈α
user-defined␈α
functions.␈α
We␈αhaven't␈α
given
␈↓ ↓H␈↓the␈α
mechanism␈α
function␈α
definition␈α
yet,␈α
but␈α
suppose␈α
a␈α
function␈α
␈↓↓subst␈↓␈α
taking␈α
three␈α∞arguments␈α
has
␈↓ ↓H␈↓been defined. It may be used in forms like ␈↓↓subst[x,y,z]␈↓ having internal form (SUBST X Y Z).
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *11
␈↓ ↓H␈↓␈↓ α_As␈α∩in␈α∩other␈α∩programming␈α∩languages␈α∪and␈α∩in␈α∩mathematics␈α∩generally,␈α∩new␈α∩forms␈α∪can␈α∩be
␈↓ ↓H␈↓constructed␈αby␈αapplying␈αthe␈αfunctions␈αand␈αpredicates␈αto␈αother␈αforms␈αand␈αnot␈αjust␈αto␈αvariables␈αand
␈↓ ↓H␈↓constants.␈α∂ Thus␈α⊂we␈α∂have␈α∂forms␈α⊂like␈α∂␈↓↓subst[x,y,␈↓αa␈α∂z␈↓↓]␈α⊂.␈α∂subst[x,y,␈↓αd␈α∂␈↓↓z]␈↓␈α⊂involving␈α∂a␈α⊂user␈α∂defined
␈↓ ↓H␈↓function ␈↓↓subst␈↓. Its internal form is (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z))).
␈↓ ↓H␈↓6. ␈↓αConditional expressions.␈↓
␈↓ ↓H␈↓␈↓ α_When␈α
the␈α
form␈α
that␈α
represents␈α
a␈α
function␈α
depends␈α
on␈α
whether␈α
a␈α
certain␈α
predicate␈α
is␈αtrue␈α
of
␈↓ ↓H␈↓the␈α⊃arguments,␈α⊃conditional␈α⊃expressions.␈α⊃ are␈α⊃used.␈α⊃ The␈α⊃conditional␈α⊃expression␈α⊃(more␈α⊂properly
␈↓ ↓H␈↓␈↓↓conditional form␈↓)
␈↓ ↓H␈↓␈↓ α_␈↓αif ␈↓↓p ␈↓αthen ␈↓↓a ␈↓αelse ␈↓↓b
␈↓ ↓H␈↓is␈α
evaluated␈α
as␈αfollows:␈α
First␈α
␈↓↓p␈↓␈α is␈α
evaluated␈α
and␈αdetermined␈α
to␈α
be␈αtrue␈α
or␈α
false.␈α If␈α
␈↓↓p␈↓␈α
is␈αtrue,
␈↓ ↓H␈↓then␈α
␈↓↓a␈↓␈α
is␈αevaluated␈α
and␈α
its␈α value␈α
is␈α
the␈α value␈α
of␈α
the␈α expression.␈α
If␈α
␈↓↓p␈↓␈α is␈α
false,␈α
then␈α ␈↓↓b␈↓␈α
is
␈↓ ↓H␈↓evaluated␈α⊃and␈α⊃its␈α⊂value␈α⊃is␈α⊃the␈α⊂value␈α⊃of␈α⊃the␈α⊂expression.␈α⊃ Note␈α⊃that␈α⊂if␈α⊃␈↓↓p␈↓␈α⊃ is␈α⊂true,␈α⊃ ␈↓↓b␈↓␈α⊃ is␈α⊂never
␈↓ ↓H␈↓evaluated,␈α∩and␈α∩if␈α∩ ␈↓↓p␈↓␈α∩ is␈α∩false,␈α∩then␈α∩ ␈↓↓a␈↓␈α∩ is␈α∩never␈α∩evaluated.␈α∩ The␈α∩importance␈α∩of␈α∩this␈α∪will␈α∩be
␈↓ ↓H␈↓explained␈α⊗ later.␈α⊗ A␈α∃familiar␈α⊗ function␈α⊗that␈α⊗can␈α∃be␈α⊗written␈α⊗conveniently␈α⊗using␈α∃conditional
␈↓ ↓H␈↓expressions is the absolute value of a real number. We have
␈↓ ↓H␈↓␈↓ α_|␈↓↓x| = ␈↓αif ␈↓↓x<0 ␈↓αthen -␈↓↓x ␈↓αelse ␈↓↓x.
␈↓ ↓H␈↓A more general form of conditional expression is
␈↓ ↓H␈↓␈↓ α_␈↓αif ␈↓↓p ␈↓αthen ␈↓↓a ␈↓αelse if ␈↓↓q ␈↓αthen ␈↓↓b . . . ␈↓αelse if ␈↓↓s ␈↓αthen ␈↓↓d ␈↓αelse ␈↓↓e.
␈↓ ↓H␈↓There␈α
can␈α
be␈α
any␈α
number␈α
of␈α
terms;␈α
the␈α
value␈α
is␈α
determined␈α
by␈α
evaluating␈αthe␈α
propositional
␈↓ ↓H␈↓terms␈α ␈↓↓p,␈α
q␈↓,␈αetc.␈αuntil␈α
a␈αtrue␈αterm␈α
is␈αfound;␈α the␈α
value␈αis␈αthen␈α
that␈αof␈αthe␈α
term␈αfollowing␈αthe␈α
next
␈↓ ↓H␈↓␈↓αthen␈↓.␈α If␈αnone␈αof␈αthe␈α
propositional␈αterms␈αis␈αtrue,␈αthen␈αthe␈α
value␈αis␈αthat␈αof␈αthe␈αterm␈α
following␈αthe
␈↓ ↓H␈↓␈↓αelse␈↓.
␈↓ ↓H␈↓␈↓ α_The function graphed in figure 4 is described by the equation
␈↓ ↓H␈↓␈↓ α_␈↓↓tri[x] = ␈↓αif ␈↓↓x<-1 ␈↓αthen 0 else if ␈↓↓x<0 ␈↓αthen 1+␈↓↓x ␈↓αelse if ␈↓↓x<1 ␈↓αthen 1-␈↓↓x ␈↓αelse 0.
␈↓"
␈↓"␈↓ ↓H␈↓␈↓ α_ ≤'`≥(0,1)␈↓ ↓H ␈αλ␈αλ `≥
␈↓"␈↓ ↓H␈↓␈↓ α_ ≤'␈αλ␈αλ `≥
␈↓"␈↓ ↓H␈↓␈↓ α_ '␈αλ (1,0)␈↓ ↓H ␈αλ
␈↓"␈↓ ↓H␈↓␈↓ α_ Figure 4.
␈↓ ↓H␈↓␈↓ α_The␈αinternal␈αform␈αof␈αconditional␈α
forms␈αis␈αmade␈αin␈αa␈α
more␈αregular␈αway␈αthan␈αthe␈α
publication
␈↓ ↓H␈↓form; the publication form was altered to conform to ALGOL. We write
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *12
␈↓ ↓H␈↓␈↓ α_(COND (p1 e1) (p2 e2) ... (pn en))
␈↓ ↓H␈↓with␈αany␈αnumber␈αof␈αterms.␈α Its␈αvalue␈αis␈αdetermined␈αby␈αevaluating␈αthe␈αp's␈αsuccessively␈αuntil␈αone␈αis
␈↓ ↓H␈↓found␈α∀which␈α∀is␈α∀true.␈α∀ Then␈α∪the␈α∀corresponding␈α∀e␈α∀is␈α∀taken␈α∪as␈α∀the␈α∀value␈α∀of␈α∀the␈α∪conditional
␈↓ ↓H␈↓expression.␈α
If␈αnone␈α
of␈αthe␈α
p's␈αis␈α
true,␈αthen␈α
the␈αvalue␈α
of␈αthe␈α
conditional␈αexpression␈α
is␈αundefined.
␈↓ ↓H␈↓Thus␈α∩all␈α∪the␈α∩e's␈α∪are␈α∩treated␈α∪the␈α∩same␈α∩which␈α∪makes␈α∩programs␈α∪for␈α∩interpreting␈α∪or␈α∩compiling
␈↓ ↓H␈↓conditional␈αexpressions␈αeasier␈αto␈αwrite.␈α Putting␈αparentheses␈αaround␈αeach␈αp-e␈αpair␈αwas␈αprobably␈αa
␈↓ ↓H␈↓mistake␈αin␈αthe␈αdesign␈αof␈αLISP,␈αbecause␈αit␈αunnecessarily␈αincreases␈αthe␈αnumber␈αof␈αright␈α
parentheses.
␈↓ ↓H␈↓It␈αshould␈αhave␈αbeen␈α (COND␈αp1␈αe1␈αp2␈αe2␈α...␈αpn␈αen),␈αbut␈αsuch␈αa␈αchange␈αshould␈αbe␈αmade␈αnow␈αonly
␈↓ ↓H␈↓as part of a massive improvement.
␈↓ ↓H␈↓␈↓ α_Conditional expressions may be compounded with functions to get forms like
␈↓ ↓H␈↓␈↓ α_␈↓αif n ␈↓↓x ␈↓αthen NIL else a ␈↓↓x . append[␈↓αd ␈↓↓x,y]␈↓
␈↓ ↓H␈↓involving a yet to be defined function ␈↓↓append␈↓. The internal form of this is
␈↓ ↓H␈↓(COND ((NULL X) NIL) (T (CONS (CAR X) (APPEND (CDR X) Y)))).
␈↓ ↓H␈↓One␈α∂would␈α∂normally␈α∂have␈α∂expected␈α∂to␈α∂write␈α∞(QUOTE␈α∂NIL)␈α∂and␈α∂(QUOTE␈α∂T),␈α∂but␈α∂there␈α∂is␈α∞a
␈↓ ↓H␈↓special␈α∂convention␈α⊂that␈α∂NIL␈α∂and␈α⊂T␈α∂may␈α∂be␈α⊂written␈α∂without␈α∂QUOTE.␈α⊂ This␈α∂means␈α⊂that␈α∂these
␈↓ ↓H␈↓symbols␈αcannot␈α
be␈αused␈αas␈α
variables.␈α The␈αvalue␈α
of␈αT␈αis␈α
the␈αpropositional␈αconstant␈α
␈↓αtrue␈↓,␈αi.e.␈α
it␈αis
␈↓ ↓H␈↓always␈αtrue␈αso␈αthat␈αit␈αis␈αimpossible␈αto␈α"fall␈α
off␈αthe␈αend"␈αof␈αa␈αconditional␈αexpression␈αwith␈αT␈α
as␈αits
␈↓ ↓H␈↓last p. It is the translation into internal form of the final ␈↓αelse␈↓ of a conditional expression.
␈↓ ↓H␈↓7. ␈↓αBoolean expressions.␈↓
␈↓ ↓H␈↓␈↓ α_In␈α⊃ making␈α⊃ up␈α⊃ the␈α⊃ propositional␈α⊂ parts␈α⊃ of␈α⊃ conditional␈α⊃expressions,␈α⊃ it␈α⊃ is␈α⊂ often
␈↓ ↓H␈↓necessary␈α
to␈α
combine␈α
elementary␈α
propositional␈α
expressions␈α
using␈α
the␈α
Boolean␈α
operators␈α
and,
␈↓ ↓H␈↓or,␈α⊃ and␈α⊃not.␈α∩ We␈α⊃ use␈α⊃ the␈α⊃ symbols␈α∩ ∧,␈α⊃ ∨,␈α⊃and␈α⊃ ¬␈α∩ respectively,␈α⊃for␈α⊃these␈α∩operators.␈α⊃ The
␈↓ ↓H␈↓Boolean␈α∩operators␈α∩may␈α⊃be␈α∩described␈α∩simply␈α⊃by␈α∩listing␈α∩the␈α⊃values␈α∩of␈α∩the␈α∩elementary␈α⊃Boolean
␈↓ ↓H␈↓expressions for each case of the arguments. Thus we have:
␈↓ ↓H␈↓␈↓ α_T∧T = T
␈↓ ↓H␈↓␈↓ α_T∧F = F
␈↓ ↓H␈↓␈↓ α_F∧T = F
␈↓ ↓H␈↓␈↓ α_F∧F = F
␈↓ ↓H␈↓␈↓ α_T∨T = T
␈↓ ↓H␈↓␈↓ α_T∨F = T
␈↓ ↓H␈↓␈↓ α_F∨T = T
␈↓ ↓H␈↓␈↓ α_F∨F = F
␈↓ ↓H␈↓␈↓ α_¬T = F
␈↓ ↓H␈↓␈↓ α_¬F = T.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *13
␈↓ ↓H␈↓␈↓ α_Since␈α⊂both␈α∂∧␈α⊂and␈α∂∨␈α⊂are␈α∂associative␈α⊂we␈α∂can␈α⊂write␈α∂Boolean␈α⊂forms␈α∂like␈α⊂␈↓↓p1∧p2∧p3␈↓␈α∂without
␈↓ ↓H␈↓worrying␈α∂about␈α∂grouping.␈α∞ The␈α∂internal␈α∂forms␈α∞are␈α∂(AND␈α∂p1␈α∂p2␈α∞...␈α∂pn),␈α∂(OR␈α∞p1␈α∂p2␈α∂...␈α∂pn)␈α∞and
␈↓ ↓H␈↓(NOT␈α⊂p).␈α⊂ It␈α⊂is␈α⊂also␈α⊂customary␈α⊂to␈α⊂use␈α⊃NIL␈α⊂instead␈α⊂of␈α⊂F,␈α⊂because␈α⊂in␈α⊂most␈α⊂LISP␈α⊃systems␈α⊂both
␈↓ ↓H␈↓Boolean␈α∞␈↓αfalse␈↓␈α∞and␈α∞the␈α∞null␈α
list␈α∞are␈α∞represented␈α∞by␈α∞the␈α
number␈α∞0.␈α∞ This␈α∞makes␈α∞certain␈α∞tests␈α
run
␈↓ ↓H␈↓faster, but introduces so many complications in implementation that it was certainly a bad idea.
␈↓ ↓H␈↓␈↓ α_The␈α
Boolean␈α
operators␈α
can␈α
be␈α
described␈α
by␈α
conditional␈α
expressions␈α
in␈α
the␈αfollowing
␈↓ ↓H␈↓way:
␈↓ ↓H␈↓␈↓ α_␈↓↓p∧q = ␈↓αif ␈↓↓p ␈↓αthen ␈↓↓q ␈↓αelse F
␈↓ ↓H␈↓␈↓ α_␈↓↓p∨q = ␈↓αif ␈↓↓p ␈↓αthen T else ␈↓↓q
␈↓ ↓H␈↓␈↓ α_¬␈↓↓p = ␈↓αif ␈↓↓p ␈↓αthen F else T.
␈↓ ↓H␈↓Note␈αthat␈αif␈α ␈↓↓p␈↓␈α is␈αfalse␈α ␈↓↓p∧q␈↓␈α is␈αfalse␈αindependent␈αof␈αthe␈αvalue␈α of␈α␈↓↓q␈↓,␈α and␈α likewise␈α if␈α ␈↓↓p␈↓␈α is␈αtrue,
␈↓ ↓H␈↓then␈α∞ ␈↓↓p∨q␈↓␈α∞ is␈α∞true␈α
independent␈α∞of␈α∞␈↓↓q␈↓.␈α∞ If␈α∞ ␈↓↓p␈↓␈α
has␈α∞been␈α∞evaluated␈α∞and␈α
found␈α∞to␈α∞be␈α∞false,␈α∞then␈α
␈↓↓q␈↓
␈↓ ↓H␈↓does␈α∂not␈α∂ have␈α∞ to␈α∂ be␈α∂evaluated␈α∂at␈α∞all␈α∂to␈α∂find␈α∂the␈α∞value␈α∂of␈α∂ ␈↓↓p∧q␈↓,␈α∂and,␈α∞in␈α∂fact,␈α∂LISP␈α∂does␈α∞not
␈↓ ↓H␈↓evaluate␈α ␈↓↓q␈↓␈α in␈αthis␈αcase.␈α Similarly,␈α ␈↓↓q␈↓␈α is␈αnot␈αevaluated␈α in␈α evaluating␈α ␈↓↓p∨q␈↓␈α if␈α ␈↓↓p␈↓␈α is␈α
true.␈αThis
␈↓ ↓H␈↓procedure␈α∂is␈α∂in␈α∂accordance␈α∂with␈α∂the␈α∂above␈α∂conditional␈α∂expression␈α∂descriptions␈α∂of␈α⊂ the␈α∂Boolean
␈↓ ↓H␈↓operators.␈α The␈αimportance␈αof␈αthis␈αconvention␈αwhich␈αcontrasts␈αwith␈αthat␈αof␈αALGOL␈α 60,␈α will␈α be
␈↓ ↓H␈↓apparent later when we discuss recursive definitions of functions and predicates.
␈↓ ↓H␈↓␈↓ α_Boolean␈α⊂expressions␈α⊂can␈α⊂be␈α⊂combined␈α⊂with␈α⊂functions␈α⊂and␈α⊂conditional␈α⊂exrpessions␈α⊃to␈α⊂get
␈↓ ↓H␈↓forms like
␈↓ ↓H␈↓␈↓ α_␈↓α if n ␈↓↓x ∨ ␈↓αn d ␈↓↓ x ␈↓αthen ␈↓↓x ␈↓αelse a ␈↓↓x . alt ␈↓αdd ␈↓↓x␈↓
␈↓ ↓H␈↓the internal form of which is
␈↓ ↓H␈↓ (COND ((OR (NULL X) (NULL (CDR X))) X) (T (CONS (CAR X) (ALT (CDDR X))))).
␈↓ ↓H␈↓8. ␈↓αRecursive function definitions.␈↓
␈↓ ↓H␈↓␈↓ α_In␈αsuch␈αlanguages␈αas␈αFORTRAN␈α
and␈α Algol,␈α computer␈α progrrams␈αare␈α expressed␈α
in␈αthe
␈↓ ↓H␈↓main␈α⊃as␈α⊃sequences␈α⊃of␈α⊃assignment␈α⊃statements␈α⊃and␈α⊃conditional␈α⊃␈↓αgo␈α⊃to␈↓'s.␈α⊃ In␈α⊃LISP,␈α⊃programs␈α⊃are
␈↓ ↓H␈↓mainly expressed in the form of recursively defined functions. We begin with an example.
␈↓ ↓H␈↓␈↓ α_The␈α
function␈α
␈↓↓alt[x]␈↓␈α
gives␈α
a␈α
list␈α
whose␈α elements␈α
are␈α
alternate␈α
elements␈α
of␈α
the␈α
list␈α ␈↓↓x␈↓
␈↓ ↓H␈↓beginning with the first. Thus
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[(A B C D E)] = (A C E),
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[(((A B) (C D))] = ((A B)),
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[(A)] = (A),
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[NIL] = NIL.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *14
␈↓ ↓H␈↓ In LISP ␈↓↓alt␈↓ is defined by the equation
␈↓ ↓H␈↓ ␈↓↓alt[x] ← ␈↓αif n ␈↓↓x ∨ ␈↓αn d ␈↓↓x ␈↓αthen ␈↓↓x ␈↓αelse a ␈↓↓x . alt[␈↓αdd ␈↓↓x]␈↓.
␈↓ ↓H␈↓In case you need a review of our precedence conventions, fully bracketed it looks like
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[x] ← ␈↓αif ␈↓↓[␈↓αn␈↓↓[x] ∨ ␈↓αn␈↓↓[␈↓αd␈↓↓[x]]] ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ [␈↓αa␈↓↓[x] . alt[␈↓αdd␈↓↓[x]]]␈↓.
␈↓ ↓H␈↓␈↓ α_This␈αdefinition␈αuses␈αno␈αways␈αof␈αforming␈αexpressions␈αthat␈αwe␈αhaven't␈αintroduced␈αpreviously,
␈↓ ↓H␈↓but␈αit␈αuses␈αthe␈αfunction␈α␈↓↓alt␈↓␈αin␈αthe␈αright␈αhand␈αside␈αof␈αits␈αown␈αdefining␈αequation.␈α To␈αget␈αthe␈αvalue
␈↓ ↓H␈↓of␈α␈↓↓alt␈α
x␈↓␈αfor␈αsome␈α
particular␈αlist,␈αwe␈α
evaluate␈αthe␈αright␈α
hand␈αside␈αof␈α
the␈αdefinition␈αsubstituting␈α
that
␈↓ ↓H␈↓list␈α
for␈α
␈↓↓x␈↓␈α
whenever␈α
␈↓↓x␈↓␈α
is␈α
encountered␈α
and␈α
re-evaluating␈α
the␈α
right␈α
hand␈α
side␈α
with␈α
a␈α
new␈α␈↓↓x␈↓␈α
whenever
␈↓ ↓H␈↓a form ␈↓↓alt e␈↓ is encountered.
␈↓ ↓H␈↓␈↓ α_This process is best explained by using it to evaluate some expressions.
␈↓ ↓H␈↓␈↓ α_Consider␈αevaluating␈α␈↓↓alt[NIL]␈↓.␈α We␈αdo␈αit␈αby␈αevaluating␈αthe␈αexpression␈αto␈αthe␈αright␈αof␈αthe␈α
←
␈↓ ↓H␈↓sign␈α∪remembering␈α∪that␈α∪the␈α∀variable␈α∪ ␈↓↓x␈↓␈α∪ has␈α∪the␈α∀value␈α∪ NIL.␈α∪ A␈α∪ conditional␈α∀expression␈α∪ is
␈↓ ↓H␈↓evaluated␈α⊃by␈α⊃first␈α⊃evaluating␈α⊂the␈α⊃first␈α⊃propositional␈α⊃term␈α⊃¬␈α⊂in␈α⊃this␈α⊃case␈α⊃ ␈↓αn␈α⊂␈↓↓x␈α⊃∨␈α⊃␈↓αn␈α⊃d␈α⊃␈↓↓x␈↓.␈α⊂This
␈↓ ↓H␈↓expression␈αis␈αa␈αdisjunction␈αand␈αin␈α its␈αevaluation␈αwe␈αfirst␈αevaluate␈αthe␈αfirst␈αdisjunct,␈αnamely␈α ␈↓αn ␈↓↓x␈↓.
␈↓ ↓H␈↓Since␈α∪ ␈↓↓x␈α∩=␈α∪NIL,␈α∩ ␈↓αn␈α∪␈↓↓x␈↓␈α∪ is␈α∩true,␈α∪the␈α∩disjunction␈α∪is␈α∪true,␈α∩and␈α∪the␈α∩value␈α∪of␈α∪ the␈α∩ conditional
␈↓ ↓H␈↓expression␈α is␈α the␈αvalue␈αof␈αthe␈αterm␈αafter␈αthe␈αfirst␈αtrue␈αpropositiional␈αterm.␈αThe␈αterm␈αis␈α ␈↓↓x␈↓,␈α and
␈↓ ↓H␈↓its␈α value␈α is␈αNIL,␈α so␈αthe␈αvalue␈αof␈α ␈↓↓alt[NIL]␈↓␈α is␈α NIL␈α as␈αstated␈αabove.␈α Obeying␈αthe␈αrules␈αabout
␈↓ ↓H␈↓the␈α
order␈αof␈α
evaluation␈α
of␈αterms␈α
in␈α
conditional␈α and␈α
Boolean␈α
expressions␈αis␈α
important␈α
in␈αthis␈α
case
␈↓ ↓H␈↓since␈αif␈αwe␈αdidn't␈αobey␈αthese␈αrules,␈αwe␈αmight␈αfind␈αourselves␈αtrying␈αto␈α evaluate␈α ␈↓αn␈αd␈α␈↓↓x␈↓␈α or␈α␈↓↓alt[␈↓αdd
␈↓ ↓H␈↓α␈↓↓x]␈↓,␈α
and␈α
since␈α
␈↓↓x␈↓␈α
is␈α
NIL,␈α
neither␈α
of␈α
these␈αhas␈α
a␈α
value.␈α
An␈α
attempt␈α
to␈α
evaluate␈α
them␈α
in␈αLISP␈α
would
␈↓ ↓H␈↓give rise to an error return.
␈↓ ↓H␈↓␈↓ α_As␈α
a␈α
second␈α∞example,␈α
consider␈α
␈↓↓alt[(A␈α
B)]␈↓.␈α∞ Since␈α
neither␈α
␈↓αn␈α∞␈↓↓x␈↓␈α
nor␈α
␈↓αn␈α
d␈α∞␈↓↓x␈↓␈α
is␈α
true␈α∞in␈α
this
␈↓ ↓H␈↓case,␈αthe␈αdisjunction␈α␈↓αn␈α␈↓↓x␈α
∨␈α␈↓αn␈αd␈α␈↓↓x␈α␈↓is␈α
false␈α and␈α the␈α value␈α of␈α
the␈α expression␈α is␈α the␈α value␈α
of
␈↓ ↓H␈↓␈↓αa␈α␈↓↓x␈α.␈αalt[␈↓αdd␈α␈↓↓x].␈α ␈↓αa␈α␈↓↓x␈↓␈α
has␈αthe␈αvalue␈α A,␈αand␈α␈↓αdd␈α␈↓↓x␈↓␈α
has␈αthe␈αvalue␈αNIL,␈αso␈αwe␈αmust␈α
now␈αevaluate
␈↓ ↓H␈↓␈↓↓alt[NIL]␈↓,␈α
and␈α
we␈α
already␈αknow␈α
that␈α
the␈α
value␈α
of␈α this␈α
expression␈α
is␈α
NIL.␈α
Therefore,␈α the
␈↓ ↓H␈↓value of ␈↓↓alt[(A B)]␈↓ is that of A.NIL which is (A).
␈↓ ↓H␈↓␈↓ α_We can describe this evaluation in a less wordy way by writing
␈↓ ↓H␈↓␈↓ α_␈↓↓alt[(A B)] = ␈↓αif n(A B) ∨ n d(A B) then (A B) else
␈↓ ↓H␈↓ ␈↓αa(A B).␈↓↓alt[␈↓αdd(A B)]
␈↓ ↓H␈↓α = if NIL ∨ n d(A B) then (A B) else
␈↓ ↓H␈↓α a(A B).␈↓↓alt[␈↓αdd(A B)]
␈↓ ↓H␈↓α = if NIL then (A B) else
␈↓ ↓H␈↓α a(A B).␈↓↓alt[␈↓αdd(A B)]
␈↓ ↓H␈↓α = a(A B).␈↓↓alt[␈↓αdd(A B)]
␈↓ ↓H␈↓α = A.␈↓↓alt[NIL]
␈↓ ↓H␈↓↓ = A.[␈↓αif nNIL ∨ ndNIL then NIL else
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *15
␈↓ ↓H␈↓α aNIL.␈↓↓alt[␈↓αddNIL]]
␈↓ ↓H␈↓α = A.[if T ∨ ndNIL then NIL else
␈↓ ↓H␈↓α aNIL.␈↓↓alt[␈↓αddNIL]]
␈↓ ↓H␈↓α = A.[if T then NIL else
␈↓ ↓H␈↓α aNIL.␈↓↓alt[␈↓αddNIL]]
␈↓ ↓H␈↓α = A.[NIL]
␈↓ ↓H␈↓α = (A).
␈↓ ↓H␈↓α␈↓ α_␈↓This␈α∂is␈α∂still␈α∂very␈α∂ long-winded,␈α∂ and␈α∞ now␈α∂ that␈α∂ the␈α∂ reader␈α∂understands␈α∂ the␈α∂ order␈α∞ of
␈↓ ↓H␈↓evaluation of conditional and Boolean expressions, we can proceed more briefly to evaluate
␈↓ ↓H␈↓α␈↓ α_␈↓↓alt[(A B C D E)] = A.alt[(C D E)]
␈↓ ↓H␈↓α = A.[C.␈↓↓alt[(E)]]
␈↓ ↓H␈↓↓ = A.[C.(E)]
␈↓ ↓H␈↓↓ = (A C E).
␈↓ ↓H␈↓↓␈↓ α_␈↓Here␈αare␈αthree␈αmore␈αexamples␈αof␈αrecursive␈αfunctions␈αand␈αtheir␈αapplication:␈αWe␈αdefine␈α ␈↓↓last␈↓
␈↓ ↓H␈↓by
␈↓ ↓H␈↓↓␈↓ α_last[x] ← ␈↓αif n d ␈↓↓x ␈↓αthen a ␈↓↓x ␈↓αelse ␈↓↓last[␈↓αd ␈↓↓x],
␈↓ ↓H␈↓↓␈↓and we compute
␈↓ ↓H␈↓␈↓ α_␈↓↓last[(A B C)] = ␈↓αif nd(A B C) then a(A B C) else ␈↓↓last[␈↓αd(A B C)]
␈↓ ↓H␈↓ = ␈↓↓last[(B C)]
␈↓ ↓H␈↓↓ = last[(C)]
␈↓ ↓H␈↓↓ = C.
␈↓ ↓H␈↓↓␈↓Clearly␈α ␈↓↓last␈↓␈α
computes␈α the␈α last␈α
element␈αof␈α
a␈αlist.␈α ␈↓↓last[NIL]␈↓␈α
is␈αundefined␈α
in␈αthe␈αLISP␈α
language;
␈↓ ↓H␈↓the␈α⊂result␈α∂of␈α⊂trying␈α∂ to␈α⊂ compute␈α⊂ it␈α∂may␈α⊂be␈α∂an␈α⊂error␈α⊂message␈α∂or␈α⊂may␈α∂be␈α⊂some␈α⊂random␈α∂result
␈↓ ↓H␈↓depending on the implementation.
␈↓ ↓H␈↓␈↓ α_The function ␈↓↓subst␈↓ is defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓subst[x, y, z] ← ␈↓αif at ␈↓↓z ␈↓αthen [if ␈↓↓z ␈↓αeq ␈↓↓y ␈↓αthen ␈↓↓x ␈↓αelse ␈↓↓z] ␈↓αelse ␈↓↓subst[x,y,␈↓αa ␈↓↓z].subst[x,y,␈↓αd ␈↓↓z]␈↓.
␈↓ ↓H␈↓We have
␈↓ ↓H␈↓␈↓ α_␈↓↓subst[(A.B), X, ((X.A).X)] =
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *16
␈↓ ↓H␈↓ = ␈↓↓subst[(A.B), X, (X.A)]subst[(A.B), X, X]
␈↓ ↓H␈↓↓ = [subst[(A.B), X, X].subst[(A.B), X, A]].(A.B)
␈↓ ↓H␈↓↓ = [[(A.B)].A].(A.B)]
␈↓ ↓H␈↓↓ = (((A.B).A).(A.B)).
␈↓ ↓H␈↓↓subst␈↓␈α⊃ computes␈α∩the␈α⊃result␈α∩of␈α⊃substituting␈α⊃the␈α∩S-expression␈α⊃ ␈↓↓x␈↓␈α∩ for␈α⊃the␈α⊃ atom␈α∩ ␈↓↓y␈↓␈α⊃ in␈α∩the␈α⊃S-
␈↓ ↓H␈↓expression ␈↓↓z␈↓. This operation is important in all kinds of symbolic computation.
␈↓ ↓H␈↓␈↓ α_The␈α⊃function␈α⊃␈↓↓append[x,y]␈↓␈α⊃which␈α⊃gives␈α⊃the␈α⊃ concatenation␈α⊃ of␈α⊃the␈α⊃lists␈α⊃␈↓↓x␈↓␈α⊃and␈α⊃␈↓↓x␈↓␈α⊃is␈α⊃also
␈↓ ↓H␈↓important.␈α It␈αis␈αalso␈αdenoted␈αby␈αthe␈αinfixed␈αexpression␈α ␈↓↓x*y␈↓␈α since␈αit␈αis␈αassociative.␈α We␈αhave␈αthe
␈↓ ↓H␈↓examples
␈↓ ↓H␈↓␈↓ α_(A B C)*(D E F) = (A B C D E F),
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_NIL*(A B) = (A B) = (A B)*NIL,
␈↓ ↓H␈↓and the formal definition
␈↓ ↓H␈↓␈↓ α_␈↓↓x*y ← ␈↓αif n ␈↓↓x ␈↓αthen ␈↓↓y ␈↓αelse [a ␈↓↓x].[[␈↓αd ␈↓↓x]*y].
␈↓ ↓H␈↓␈↓ α_The␈α
Boolean␈α∞ operations␈α
can␈α∞also␈α
be␈α
used␈α∞in␈α
making␈α∞recursive␈α
definitions␈α∞ provided␈α
we
␈↓ ↓H␈↓observe the order of evaluation of constituents. First, we define a predicate ␈↓↓equal␈↓ by
␈↓ ↓H␈↓␈↓ α_␈↓↓equal[x,y] ← x ␈↓αeq ␈↓↓y ∨ [¬␈↓αat ␈↓↓x ∧ ¬␈↓αat ␈↓↓y ∧ equal[␈↓αa ␈↓↓x,␈↓αa ␈↓↓y] ∧ equal[␈↓αd ␈↓↓x, ␈↓αd ␈↓↓y]]␈↓.
␈↓ ↓H␈↓␈↓↓equal[x,y]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α ␈↓↓x␈↓␈α and␈α ␈↓↓y␈↓␈α are␈α the␈α same␈αS-expression,␈α and␈α the␈α use␈α of␈αthis
␈↓ ↓H␈↓predicate␈αmakes␈αup␈αfor␈αthe␈αfact␈αthat␈αthe␈αbasic␈αpredicate␈α ␈↓αeq␈↓␈α is␈αguaranteed␈α to␈α test␈α equality␈α only
␈↓ ↓H␈↓when one of the operands is known to be an atom. We shall also use the infixes = and ≠.
␈↓ ↓H␈↓␈↓ α_Membership of an S-expression ␈↓↓x␈↓ in a list ␈↓↓y␈↓ is tested by
␈↓ ↓H␈↓␈↓ α_␈↓↓member[x, y] ← ¬␈↓αn ␈↓↓y ∧ [[x = ␈↓αa ␈↓↓y] ∨ member[x, ␈↓αd ␈↓↓y]].
␈↓ ↓H␈↓This relation is also denoted by ␈↓↓x ε y␈↓. Her? are some computations:
␈↓ ↓H␈↓␈↓ α_␈↓↓member[B, (A B)] = ¬␈↓αn (A B) ∧ [[B = a (A B)]
␈↓ ↓H␈↓ ∨ ␈↓↓member[B, ␈↓αd (A B)]],
␈↓ ↓H␈↓α = ␈↓↓member[B, (B)]
␈↓ ↓H␈↓↓ = T.
␈↓ ↓H␈↓↓␈↓ α_␈↓Sometimes␈α a␈α
function␈α is␈αdefined␈α
with␈αthe␈α
help␈αof␈αauxiliary␈α
functions.␈α Thus,␈α
the␈αreverse
␈↓ ↓H␈↓of a list ␈↓↓x␈↓ , (e.g. ␈↓↓reverse[(A B C D)] = (D C B A)␈↓), is given by
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *17
␈↓ ↓H␈↓↓␈↓ α_reverse[x] ← rev[x, NIL]
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓rev[x, y] ← ␈↓αif n ␈↓↓x ␈↓αthen ␈↓↓y ␈↓αelse ␈↓↓rev[␈↓αd ␈↓↓x, [␈↓αa ␈↓↓x].y].
␈↓ ↓H␈↓A computation is
␈↓ ↓H␈↓␈↓ α_␈↓↓reverse[(A B C)] = rev[(A B C), NIL]
␈↓ ↓H␈↓ = ␈↓↓rev[(B C), (A)]
␈↓ ↓H␈↓↓ = rev[(C), (B A)]
␈↓ ↓H␈↓↓ = rev[NIL, (C B A)]
␈↓ ↓H␈↓↓ = (C B A).
␈↓ ↓H␈↓↓␈↓ α_␈↓A more elaborate example of recursive definition is given by
␈↓ ↓H␈↓↓␈↓ α_flatten[x] ← flat[x, NIL]
␈↓ ↓H␈↓↓␈↓ α_flat[x, y] ← ␈↓αif at ␈↓↓x ␈↓αthen ␈↓↓x.y
␈↓ ↓H␈↓↓ ␈↓αelse ␈↓↓flat[␈↓αa ␈↓↓x, flat[␈↓αd ␈↓↓x, y]].
␈↓ ↓H␈↓↓␈↓We have
␈↓ ↓H␈↓␈↓ α_␈↓↓flatten[((A.B).C)] = flat[((A.B).C), NIL]
␈↓ ↓H␈↓ = ␈↓↓flat[(A.B), flat[C, NIL]]
␈↓ ↓H␈↓↓ = flat[(A.B), (C)]
␈↓ ↓H␈↓↓ = flat[A, flat[B, (C)]]
␈↓ ↓H␈↓↓ = flat[A, (B C)]
␈↓ ↓H␈↓↓ = (A B C).
␈↓ ↓H␈↓↓␈↓The␈α reader␈α will␈αsee␈αthat␈αthe␈αvalue␈αof␈α ␈↓↓flatten[x]␈↓␈α is␈αa␈αlist␈αof␈αthe␈αatoms␈α of␈α the␈α S-expression␈α ␈↓↓x␈↓
␈↓ ↓H␈↓from left to write. Thus ␈↓↓flatten[((A B) A)] = (A B NIL A NIL)␈↓.
␈↓ ↓H␈↓␈↓ α_Many␈α⊂ functions␈α⊂ can␈α∂be␈α⊂conveniently␈α⊂written␈α∂in␈α⊂more␈α⊂than␈α∂one␈α⊂way.␈α⊂ For␈α⊂example,␈α∂the
␈↓ ↓H␈↓function ␈↓↓reverse␈↓ mentioned above can be written without an auxiliary function as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓reverse[x] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else ␈↓↓reverse[␈↓αd ␈↓↓x]*<a x>
␈↓ ↓H␈↓but it will be explained later that the earlier definition involves less computation.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *18
␈↓ ↓H␈↓␈↓ α_The␈αuse␈α
of␈αconditional␈α
expressions␈α for␈α recursive␈α
function␈αdefinition␈α
is␈α not␈α
limited␈α to
␈↓ ↓H␈↓functions␈α∂ of␈α∞ S-expressions.␈α∂ For␈α∞example,␈α∂the␈α∞factorial␈α∂function␈α∞and␈α∂the␈α∂Euclidean␈α∞algorithm
␈↓ ↓H␈↓for the greatest common divisor are expressed as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓n! ← ␈↓αif ␈↓↓n=0 ␈↓αthen 1 else ␈↓↓n(n-1)!
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓gcd(m, n) ← ␈↓αif ␈↓↓m>n ␈↓αthen ␈↓↓gcd(n, m) ␈↓αelse if ␈↓↓m=0 ␈↓αthen ␈↓↓n
␈↓ ↓H␈↓ ␈↓αelse ␈↓↓gcd(n mod m, m)
␈↓ ↓H␈↓↓␈↓where␈α ␈↓↓n␈αmod␈αm␈↓␈α denotes␈αthe␈αremainder␈αwhen␈α ␈↓↓n␈↓␈α is␈αdivided␈αby␈α ␈↓↓m␈↓␈α and␈αmay␈αitself␈αbe␈αexpressed
␈↓ ↓H␈↓recursively by
␈↓ ↓H␈↓␈↓ α_␈↓↓n mod m ← ␈↓αif ␈↓↓n<m ␈↓αthen ␈↓↓n ␈↓αelse (␈↓↓n-m) mod m.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α
internal␈α
form␈α
of␈α
function␈αdefinitions␈α
depends␈α
on␈α
the␈α
implementation.␈α
Stanford␈αLISP
␈↓ ↓H␈↓and UCI LISP for the PDP-10 computer use the form
␈↓ ↓H␈↓␈↓ α_(DE <function name> <list of variables> <right hand side>).
␈↓ ↓H␈↓In these LISPs, the above definition of ␈↓↓subst␈↓ is
␈↓ ↓H␈↓␈↓ α_(DE␈α⊂SUBST␈α⊂(X␈α⊂Y␈α⊂Z)␈α⊂(COND␈α⊂((ATOM␈α⊂Z)␈α∂(COND␈α⊂((EQ␈α⊂Z␈α⊂X)␈α⊂Y)␈α⊂(T␈α⊂Z)))␈α⊂(T␈α∂(CONS
␈↓ ↓H␈↓(SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))),
␈↓ ↓H␈↓and the definition of ␈↓↓alt␈↓ is
␈↓ ↓H␈↓␈↓ α_(DE␈αALT␈α(X)␈α
(COND␈α((OR␈α(NULL␈αX)␈α
(NULL␈α(CDR␈αX)))␈αX)␈α
(T␈α(CONS␈α(CAR␈αX)␈α
(ALT
␈↓ ↓H␈↓(CDDR X)))))).
␈↓ ↓H␈↓␈↓ α_Another␈α
notation␈α
for␈α
function␈α
definition␈α∞called␈α
the␈α
DEFPROP␈α
notation␈α
will␈α∞be␈α
explained
␈↓ ↓H␈↓after λ-expressions have been introduced.
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓␈↓ α_1. Consider the function ␈↓↓drop␈↓ defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓drop[x] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else <a ␈↓↓x>.drop[␈↓αd ␈↓↓x].
␈↓ ↓H␈↓Compute␈α∞(by␈α∞hand)␈α∞ ␈↓↓drop[(A␈α∞B␈α∂C)]␈↓.␈α∞ What␈α∞does␈α∞ ␈↓↓drop␈↓␈α∞ do␈α∂ to␈α∞ lists␈α∞ in␈α∞general?␈α∞ Write␈α∂␈↓↓drop␈↓␈α∞in
␈↓ ↓H␈↓internal notation using DE.
␈↓ ↓H␈↓␈↓ α_2. What does the function
␈↓ ↓H␈↓␈↓ α_␈↓↓r2[x] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else ␈↓↓reverse[␈↓αa ␈↓↓x].r2[␈↓αd ␈↓↓x]
␈↓ ↓H␈↓do to lists of lists? How about
␈↓ ↓H␈↓␈↓ α_␈↓↓r3[x] ← ␈↓αif at ␈↓↓x ␈↓αthen ␈↓↓x ␈↓αelse ␈↓↓reverse[r4[x]]
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *19
␈↓ ↓H␈↓␈↓ α_␈↓↓r4[x] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else ␈↓↓r3[␈↓αa ␈↓↓x].r4[␈↓αd ␈↓↓x]?
␈↓ ↓H␈↓␈↓ α_3. Compare
␈↓ ↓H␈↓␈↓ α_␈↓↓r3'[x] ← ␈↓αif at ␈↓↓x ␈↓αthen ␈↓↓x ␈↓αelse ␈↓↓r3'[␈↓αd ␈↓↓x]*<r3'[␈↓αa ␈↓↓x]>
␈↓ ↓H␈↓with the function ␈↓↓r3␈↓ of the preceding example.
␈↓ ↓H␈↓␈↓ α_4. Consider ␈↓↓r5␈↓ defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓r5[x] ← ␈↓αif n ␈↓↓x ∨ ␈↓αn d ␈↓↓x ␈↓αthen ␈↓↓x
␈↓ ↓H␈↓ ␈↓αelse [a ␈↓↓r5[␈↓αd ␈↓↓x]]
␈↓ ↓H␈↓↓ . r5[␈↓αa ␈↓↓x . r5[␈↓αd ␈↓↓r5[␈↓αd ␈↓↓x]]].
␈↓ ↓H␈↓↓␈↓Compute␈α ␈↓↓r5[(A␈αB␈αC␈αD)]␈↓.␈α What␈αdoes␈α r5␈α do␈α
in␈αgeneral?␈α Needless␈α to␈αsay,␈αthis␈αis␈αnot␈αa␈αgood␈α
way
␈↓ ↓H␈↓of computing this function even though it involves no auxiliary functions.
␈↓ ↓H␈↓9. ␈↓αLambda expressions and functions with functions as arguments.␈↓
␈↓ ↓H␈↓␈↓ α_It␈α⊂is␈α⊂common␈α⊂to␈α⊂use␈α⊂phrases␈α⊂like␈α⊂"the␈α⊂function␈α⊂ 2␈↓↓x+y␈↓".␈α⊂ This␈α⊂is␈α⊂ not␈α⊂a␈α⊂precise␈α⊂notation
␈↓ ↓H␈↓because␈α
we␈α
cannot␈αsay␈α
2␈↓↓x+y(3,␈α
4)␈↓␈α
and␈αknow␈α
whether␈α
the␈α
desired␈α result␈α
is␈α
2␈↓#
.␈↓#3+4␈α
or␈α 2␈↓#
.␈↓#4+3
␈↓ ↓H␈↓regarding␈α
the␈α
expression␈α
as␈α
a␈α
function␈α
of␈α
two␈α
variables.␈α
Worse␈α
yet,␈α
we␈α
might␈α
have␈α
meant␈α
a␈α
one-
␈↓ ↓H␈↓variable function of ␈↓↓x␈↓ wherein ␈↓↓y␈↓ is regarded as a parameter.
␈↓ ↓H␈↓␈↓ α_The␈α∞problem␈α
of␈α∞ giving␈α
names␈α∞ to␈α
functions␈α∞ is␈α
solved␈α∞ by␈α
Church's␈α∞ λ-notation.␈α
In
␈↓ ↓H␈↓the␈α∞ above␈α∞ example,␈α∞ we␈α∞ would␈α∞ write␈α∞λ␈↓↓x␈α∞y:␈α∞2x+y␈↓␈α∞ to␈α∞denote␈α∞ the␈α∞ function␈α∞ of␈α∂ two␈α∞ variables
␈↓ ↓H␈↓with␈α⊂ first␈α⊂argument␈α∂ ␈↓↓x␈↓␈α⊂ and␈α⊂ second␈α⊂ argument␈α∂ ␈↓↓y␈↓␈α⊂ whose␈α⊂value␈α∂is␈α⊂given␈α⊂by␈α⊂the␈α∂expression
␈↓ ↓H␈↓2␈↓↓x+y␈↓. Thus,
␈↓ ↓H␈↓␈↓ α_[λ␈↓↓x y: 2x+y][3, 4] = 10␈↓.
␈↓ ↓H␈↓Likewise,␈α∂ [λ␈↓↓y␈α∂x:␈α∂2x+y][3,␈α∂4]␈α∂=␈α∂11␈↓.␈α⊂ Like␈α∂variables␈α∂of␈α∂integration␈α∂and␈α∂the␈α∂bound␈α⊂variables␈α∂of
␈↓ ↓H␈↓quantifiers␈αin␈αlogic,␈αvariables␈αfollowing␈α λ␈αare␈α bound␈α or␈αdummy␈αand␈αthe␈αexpression␈αas␈αa␈αwhole
␈↓ ↓H␈↓may␈αbe␈αreplaced␈α
by␈αany␈αothers␈αprovided␈α
the␈αreplacement␈αis␈αdone␈α
consistently␈αand␈αdoes␈α
not␈αmake
␈↓ ↓H␈↓any␈α∞ variable␈α∞ bound␈α
by␈α∞ λ␈α∞ the␈α
same␈α∞as␈α∞a␈α∞free␈α
variable␈α∞in␈α∞the␈α
expression.␈α∞ Thus␈α∞ λ␈↓↓x␈α∞y:␈α
2x+y␈↓
␈↓ ↓H␈↓represents␈α⊃ the␈α⊃ same␈α⊃ function␈α∩ as␈α⊃λ␈↓↓y␈α⊃x:␈α⊃2y+x␈↓␈α∩or␈α⊃λ␈↓↓u␈α⊃v:␈α⊃2u+v␈↓␈α⊃,␈α∩but␈α⊃in␈α⊃the␈α⊃function␈α∩of␈α⊃one
␈↓ ↓H␈↓argument λ␈↓↓x: 2x+y␈↓ , we cannot replace the variable ␈↓↓x␈↓ by ␈↓↓y␈↓ , though we could replace it by ␈↓↓u␈↓.
␈↓ ↓H␈↓␈↓ α_λ-notation␈α∃plays␈α∃two␈α∃important␈α∃ roles␈α∃ in␈α∃ LISP.␈α∃ First,␈α∃ it␈α∃allows␈α∃us␈α∃to␈α⊗rewrite␈α∃an
␈↓ ↓H␈↓expression␈αcontaining␈αtwo␈αor␈αmore␈αoccurrences␈αof␈αthe␈αsame␈αsub-expression␈αin␈αsuch␈αa␈αway␈αthat␈αthe
␈↓ ↓H␈↓expression␈α
occurs␈αonly␈α
once.␈α
Thus␈α (2␈↓↓x+1)␈↓¬4+3(2␈↓↓x+1)␈↓¬3␈↓␈α
can␈α
be␈αwritten␈α
[λ␈↓↓w:␈αw␈↓¬4+3␈↓↓w␈↓¬3][2␈↓↓x+1]␈↓.␈α
This
␈↓ ↓H␈↓can␈α
save␈α
considerable␈α
computation,␈α
and␈α
corresponds␈αto␈α
the␈α
practice␈α
in␈α
ordinary␈α
programming␈αof
␈↓ ↓H␈↓assigning␈α∀to␈α∪a␈α∀variable␈α∪the␈α∀value␈α∪of␈α∀a␈α∪sub-expression␈α∀that␈α∪occurs␈α∀more␈α∪than␈α∀once␈α∀ in␈α∪an
␈↓ ↓H␈↓expression and then writing the expression in terms of the variable.
␈↓ ↓H␈↓␈↓ α_The␈α
second␈α
use␈α
of␈α
λ-expressions␈α
is␈α
in␈α
using␈α
functions␈α
that␈α
take␈α
functions␈α
as␈αarguments.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *20
␈↓ ↓H␈↓Suppose␈αwe␈αwant␈α
to␈αform␈αa␈αnew␈α
list␈αfrom␈αan␈αold␈α
one␈αby␈αapplying␈αa␈α
function␈α ␈↓↓f␈↓␈α to␈α
each␈αelement
␈↓ ↓H␈↓of the list. This can be done using the function ␈↓↓mapcar␈↓ defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓mapcar[x, f] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else ␈↓↓f[␈↓αa ␈↓↓x] . mapcar[␈↓αd ␈↓↓x, f].
␈↓ ↓H␈↓Suppose␈αthe␈αoperation␈αwe␈α
want␈αto␈αperform␈αis␈α
squaring,␈αand␈αwe␈αwant␈α
to␈αapply␈αit␈αto␈α
the␈αlist␈α (1␈α2␈α
3
␈↓ ↓H␈↓4 5 6 7). We have
␈↓ ↓H␈↓␈↓ α_␈↓↓mapcar[(1 2 3 4 5 6 7), λx: x␈↓¬2] = (1 4 9 16 25 36 49).
␈↓ ↓H␈↓␈↓ α_A␈α⊃more␈α⊃generally␈α⊃useful␈α⊃operation␈α⊃than␈α⊃ ␈↓↓mapcar␈↓␈α⊃is␈α⊃␈↓↓maplist␈↓␈α⊃in␈α⊃ which␈α⊃ the␈α⊃ function␈α⊂is
␈↓ ↓H␈↓applied to the successive sublists of the list rather than to the elements. ␈↓↓maplist␈↓ is defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓maplist[x, f] ← ␈↓αif n ␈↓↓x ␈↓αthen NIL else ␈↓↓f[x] . maplist[␈↓αd ␈↓↓x, f].
␈↓ ↓H␈↓␈↓ α_As␈α
an␈α
application␈α
of␈α
␈↓↓maplist␈↓␈α
and␈αfunctional␈α
arguments,␈α
we␈α
shall␈α
define␈α
a␈α
function␈α for
␈↓ ↓H␈↓differentiating␈α algebraic␈α
expressions␈αinvolving␈α
sums␈αand␈α
products.␈α The␈α
expressions␈αare␈αbuilt␈α
up
␈↓ ↓H␈↓from atoms denoting variables and integer constants according to the syntax
␈↓ ↓H␈↓␈↓ α_<expression> ::= <variable> | <integer> |
␈↓ ↓H␈↓ (PLUS <explist>) | (TIMES <explist>)
␈↓ ↓H␈↓␈↓ α_<explist> ::= <expression> | <expression><explist>
␈↓ ↓H␈↓Here,␈α
PLUS␈α
followed␈αby␈α
a␈α
list␈α
of␈αarguments␈α
denotes␈α
the␈α
sum␈αof␈α
these␈α
arguments␈α
and␈α TIMES
␈↓ ↓H␈↓followed␈α∂by␈α∂a␈α∂list␈α∂of␈α∂arguments␈α∂ denotes␈α∂ their␈α∂product.␈α∂ The␈α∂ function␈α∂ ␈↓↓diff[e,␈α∂v]␈↓␈α⊂ gives␈α∂the
␈↓ ↓H␈↓partial derivative of the expression ␈↓↓e␈↓ with respect to the variable ␈↓↓v␈↓. We have
␈↓ ↓H␈↓␈↓ α_␈↓↓diff[e, v] ← ␈↓αif at ␈↓↓e ␈↓αthen [if ␈↓↓e ␈↓αeq ␈↓↓v ␈↓αthen 1 else 0]
␈↓ ↓H␈↓ ␈↓αelse if a ␈↓↓e ␈↓αeq PLUS then
␈↓ ↓H␈↓α PLUS . ␈↓↓mapcar[␈↓αd ␈↓↓e, λx: diff[x, v] ]
␈↓ ↓H␈↓↓ ␈↓αelse if a ␈↓↓e ␈↓αeq TIMES ␈↓↓then
␈↓ ↓H␈↓↓ PLUS.maplist[␈↓αd ␈↓↓e, λx: TIMES
␈↓ ↓H␈↓↓ . maplist[␈↓αd ␈↓↓e, λy: ␈↓αif ␈↓↓x ␈↓αeq ␈↓↓y ␈↓αthen
␈↓ ↓H␈↓α ␈↓↓diff[␈↓αa ␈↓↓y, v] ␈↓αelse a ␈↓↓y]].
␈↓ ↓H␈↓↓␈↓The term that describes the rule for differentiating products corresponds to the rule
␈↓ ↓H␈↓and␈α ␈↓↓maplist␈↓␈α has␈α to␈αbe␈α
used␈αrather␈αthan␈α ␈↓↓mapcar␈↓␈α since␈α
whether␈αto␈αdifferentiate␈αin␈αforming␈α
the
␈↓ ↓H␈↓product␈α
is␈α
determined␈α
by␈α
equality␈α
of␈α
the␈α
indices␈α ␈↓↓i␈↓␈α
and␈α
␈↓↓j␈↓␈α
rather␈α
than␈α
equality␈α
of␈α
the␈αterms␈α
␈↓↓e␈↓∧i␈↓
␈↓ ↓H␈↓and ␈↓↓e␈↓∧j␈↓.
␈↓ ↓H␈↓␈↓ α_Two␈α⊂more␈α⊂useful␈α⊂functions␈α⊂with␈α⊂functions␈α⊂as␈α⊂arguments␈α⊂are␈α⊂the␈α⊂predicates␈α⊃ ␈↓↓andlis␈↓␈α⊂ and
␈↓ ↓H␈↓␈↓↓orlis␈↓ defined by the equations
␈↓ ↓H␈↓␈↓ α_␈↓↓andlis[u, p] ← ␈↓αn ␈↓↓u ∨ [p[␈↓αa ␈↓↓u] ∧ andlis[␈↓αd ␈↓↓u, p]]
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *21
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓orlis[u, p] ← ¬␈↓αn ␈↓↓u ∧ [p[␈↓αa ␈↓↓u] ∨ orlis[␈↓αd ␈↓↓u, p]].
␈↓ ↓H␈↓␈↓ α_The internal form for a λ-expression is
␈↓ ↓H␈↓␈↓ α_(LAMBDA <list of variables> <expression to be evaluated>).
␈↓ ↓H␈↓Thus␈αλ␈αx:␈αdiff[x,v]␈αis␈αwritten␈α(LAMBDA␈α(X␈αV)␈α(DIFF␈αX␈αV)).␈α When␈αa␈αfunction␈αspecified␈αwith␈α
λ
␈↓ ↓H␈↓is␈α∞written␈α∞as␈α
an␈α∞argument␈α∞of␈α∞a␈α
function␈α∞taking␈α∞a␈α∞functional␈α
argument,␈α∞then␈α∞the␈α∞λ-expression␈α
is
␈↓ ↓H␈↓marked with the identifier FUNCTION. Thus the above definition of ␈↓↓diff␈↓ translates to
␈↓ ↓H␈↓␈↓ α_(DE DIFF (E V) (COND
␈↓ ↓H␈↓ ((ATOM E) (COND ((EQ E V) 1) (T 0)))
␈↓ ↓H␈↓ ((EQ (CAR E) (QUOTE PLUS))
␈↓ ↓H␈↓ (CONS␈α(QUOTE␈αPLUS)␈α(MAPCAR␈α(CDR␈αE)␈α(FUNCTION␈α(LAMBDA␈α(X)
␈↓ ↓H␈↓(DIFF X V))))))
␈↓ ↓H␈↓ ((EQ␈α⊗(CAR␈α∃E)␈α⊗(QUOTE␈α∃TIMES))␈α⊗ (CONS␈α⊗(QUOTE␈α∃PLUS)
␈↓ ↓H␈↓(MAPLIST␈α(CDR␈αE)␈α
(FUNCTION␈α(LAMBDA␈α(X)␈α
(CONS␈α(QUOTE␈αTIMES)␈α(MAPLIS␈α
(CDR
␈↓ ↓H␈↓E) (FUNCTION (LAMBDA (Y) (COND ((EQ X Y) (DIFF (CAR Y) V)) (T (CAR Y)))))))))))))).
␈↓ ↓H␈↓␈↓ α_Another␈αway␈αof␈αwriting␈αfunction␈αdefinitions␈αin␈αinternal␈αnotation␈αuses␈αLAMBDA␈αto␈αmake␈αa
␈↓ ↓H␈↓function of the right side of a definition.
␈↓ ↓H␈↓It is like writing ␈↓↓subst␈↓ and ␈↓↓alt␈↓ as
␈↓ ↓H␈↓␈↓ α_␈↓↓subst = λx y z.[␈↓αif at ␈↓↓z ␈↓αthen ␈↓↓[␈↓αif ␈↓↓z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse ␈↓↓z] ␈↓αelse ␈↓↓subst[x,y,␈↓αa␈↓↓ z] . subst[x,y,␈↓αd␈↓↓ z]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓alt = λx.[␈↓αif n ␈↓↓x ∨ ␈↓αn d ␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse a ␈↓↓x .alt ␈↓αdd␈↓↓ x]␈↓.
␈↓ ↓H␈↓The definitions of ␈↓↓subst␈↓ and ␈↓↓alt␈↓ take the forms
␈↓ ↓H␈↓␈↓ α_(DEFPROP␈αSUBST␈α(LAMBDA␈α
(X␈αY␈αZ)␈α
(COND␈α((ATOM␈αZ)␈α(COND␈α
((EQ␈αZ␈αX)␈α
Y)␈α(T
␈↓ ↓H␈↓Z))) (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z))))))),
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_(DEFPROP␈α⊂ALT␈α⊂(LAMBDA␈α⊂(X)␈α∂(COND␈α⊂((OR␈α⊂(NULL␈α⊂X)␈α∂(NULL␈α⊂(CDR␈α⊂X)))␈α⊂X)␈α∂(T
␈↓ ↓H␈↓(CONS (CAR X) (ALT (CDDR X))))))).
␈↓ ↓H␈↓␈↓ α_The␈α∞general␈α∞form␈α∂is␈α∞(DEFPROP␈α∞<function␈α∞name>␈α∂<defining␈α∞λ-expression>)␈α∞and␈α∂is␈α∞often
␈↓ ↓H␈↓used by programs that output LISP.
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *22
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓␈↓ α_1.␈α∂Compute␈α∂ ␈↓↓diff[(TIMES␈α∂X␈α∂(PLUS␈α∂Y␈α∂1)␈α∞3),␈α∂X]␈↓␈α∂ using␈α∂ the␈α∂ above␈α∂definition␈α∂ of␈α∞ ␈↓↓diff␈↓.
␈↓ ↓H␈↓Now do you see why algebraic simplification is important?
␈↓ ↓H␈↓␈↓ α_2. Compute ␈↓↓orlis[((A B) (C D) E), ␈↓αat]␈↓.
␈↓ ↓H␈↓10. ␈↓αLabel.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∀λ␈α∪mechanism␈α∀is␈α∀ not␈α∪ adequate␈α∀ for␈α∀ providing␈α∪ names␈α∀ for␈α∀recursive␈α∪ functions,
␈↓ ↓H␈↓because␈α in␈αthis␈αcase␈α
there␈αhas␈αto␈αbe␈αa␈α
way␈αof␈αreferring␈αto␈α
the␈αfunction␈αname␈αwithin␈αthe␈α
function.
␈↓ ↓H␈↓Therefore,␈α
we␈α
use␈α
the␈α
notation␈α
␈↓αlabel[␈↓↓f,␈α
e]␈↓␈α to␈α
denote␈α
the␈α
expression␈α
␈↓↓e␈↓␈α
but␈α
where␈αoccurrences␈α
of
␈↓ ↓H␈↓␈↓↓f␈↓␈α⊂ within␈α⊂ ␈↓↓e␈↓␈α⊂ refer␈α⊂ to␈α⊂ the␈α⊂ whole␈α⊂ expression.␈α⊂ For␈α⊂example,␈α⊂ suppose␈α⊂we␈α⊂wished␈α⊂to␈α⊃define␈α⊂a
␈↓ ↓H␈↓function␈αthat␈αtakes␈α
alternate␈αelements␈αof␈α
each␈αelement␈αof␈αa␈α
list␈αand␈αmakes␈α
a␈αlist␈αof␈α
these.␈α Thus,
␈↓ ↓H␈↓we want
␈↓ ↓H␈↓␈↓ α_␈↓↓glub[((A B C) (A B C D) (X Y Z))] = ((A C) (A C) (X Z)).␈↓
␈↓ ↓H␈↓We can make the definition
␈↓ ↓H␈↓␈↓ α_␈↓↓glub[x]␈α←␈αmapcar[x,␈α␈↓αlabel[␈↓↓alt,␈αλx:␈α␈↓αif␈αn␈α␈↓↓x␈α∨␈α␈↓αn␈αd␈α␈↓↓x␈α␈↓αthen␈α␈↓↓x␈α
␈↓αelse
␈↓ ↓H␈↓αa ␈↓↓x . alt[␈↓αdd ␈↓↓x]]].
␈↓ ↓H␈↓␈↓ α_The␈α∂internal␈α⊂form␈α∂of␈α⊂␈↓↓label[<name>,<function␈α∂expression>]␈↓␈α⊂is␈α∂(LABEL␈α⊂<name>␈α∂<function
␈↓ ↓H␈↓expression>), so that the internal form of the definition of ␈↓↓glub␈↓ is
␈↓ ↓H␈↓␈↓ α_(DE␈α∞GLUB␈α∂(X)␈α∞(MAPCAR␈α∞X␈α∂(LABEL␈α∞ALT␈α∞(LAMBDA␈α∂(X)␈α∞(COND␈α∞(OR␈α∂(NULL␈α∞X)
␈↓ ↓H␈↓(NULL (CDR X))) X) (T (CONS (CAR X) (ALT (CDDR X)))))))).
␈↓ ↓H␈↓␈↓ α_The␈α∂identifier␈α∂ ␈↓↓alt␈↓␈α∂ in␈α∞the␈α∂above␈α∂example␈α∂is␈α∞bound␈α∂by␈α∂the␈α∂ label␈α∞ and␈α∂is␈α∂ local␈α∂ to␈α∞ that
␈↓ ↓H␈↓expression,␈α and␈α this␈αis␈αthe␈αgeneral␈αrule.␈α The␈αlabel␈α construction␈αis␈αnot␈αoften␈αused␈αin␈αLISP␈αsince
␈↓ ↓H␈↓it␈αis␈αmore␈αusual␈αto␈α give␈αfunctions␈αglobal␈α
definitions.␈αD.␈αM.␈αR.␈αPark␈αpointed␈αout␈αthat␈αif␈α
we␈αallow
␈↓ ↓H␈↓variables␈α∞to␈α∞represent␈α
functions␈α∞and␈α∞use␈α∞a␈α
suitable␈α∞ λ␈α∞construction,␈α∞the␈α
use␈α∞of␈α∞ label␈α∞ could␈α
be
␈↓ ↓H␈↓avoided.
␈↓ ↓H␈↓11. ␈↓αNumerical computation.␈↓
␈↓ ↓H␈↓␈↓ α_Numerical␈α
calculation␈α
and␈α
symbolic␈α
calculation␈α
must␈α
often␈α
be␈α
combined,␈α
so␈α
LISP␈αprovides
␈↓ ↓H␈↓for numerical computation also.
␈↓ ↓H␈↓␈↓ α_In␈αthe␈α
first␈αplace,␈α
we␈αneed␈α
to␈αinclude␈α
numbers␈αas␈α
parts␈αof␈α
symbolic␈αexpressions.␈α
LISP␈αhas
␈↓ ↓H␈↓both␈α
integer␈α∞and␈α
floating␈α
point␈α∞numbers␈α
which␈α
are␈α∞regarded␈α
as␈α
atoms.␈α∞ These␈α
numbers␈α∞may␈α
be
␈↓ ↓H␈↓included␈αas␈α
atoms␈αin␈α
writing␈αS-expressions.␈α Thus␈α
we␈αcan␈α
have␈αthe␈αlists␈α
(1␈α3␈α
5),␈α(3.5␈α
6.1␈α-7.2E9),
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *23
␈↓ ↓H␈↓and␈α(PLUS␈α
X␈α1.3);␈αthe␈α
first␈αis␈αa␈α
list␈αof␈α
integers,␈αthe␈αsecond␈α
a␈αlist␈αof␈α
floating␈αpoint␈α
numbers,␈αand
␈↓ ↓H␈↓the␈α⊃third␈α⊃a␈α⊃symbolic␈α⊃list␈α⊃containing␈α⊃both␈α⊃numberical␈α⊃and␈α⊃non-numerical␈α⊃atoms.␈α∩ Integers␈α⊃are
␈↓ ↓H␈↓written␈α
without␈α
decimal␈α
points␈α
which␈α
are␈α
used␈α
to␈α
signal␈α
floating␈α
point␈α
numbers.␈α
As␈α∞in␈α
Fortran,
␈↓ ↓H␈↓the␈α
letter␈αE␈α
is␈αused␈α
to␈αsignal␈α
the␈αexponent␈α
of␈αa␈α
floating␈αpoint␈α
number␈αwhich␈α
is␈αa␈α
signed␈αinteger.
␈↓ ↓H␈↓The␈αsizes␈α
of␈αnumbers␈α
admitted␈αdepends␈αon␈α
the␈αimplementation.␈α
When␈αa␈αdotted␈α
pair,␈αsay␈α
(1␈α.␈α2)␈α
is
␈↓ ↓H␈↓wanted,␈α⊂the␈α⊂spaces␈α⊂around␈α⊂the␈α⊂dot␈α⊂distinguish␈α⊂it␈α∂from␈α⊂the␈α⊂list␈α⊂(1.2)␈α⊂whose␈α⊂sole␈α⊂element␈α⊂is␈α∂the
␈↓ ↓H␈↓floating point number 1.2.
␈↓ ↓H␈↓␈↓ α_In␈α
publication␈αlanguage␈α
we␈α
will␈αuse␈α
ordinary␈α
mathematical␈αnotation␈α
for␈αnumerical␈α
functions.
␈↓ ↓H␈↓For␈αexponentiation␈αwe␈αwill␈αuse␈αthe␈αusual␈αsuperscript␈αnotation␈α␈↓↓x␈↓¬y␈↓␈αwhen␈αtypographically␈αconvenient
␈↓ ↓H␈↓and␈α∞the␈α∞linear␈α∞Algol␈α∞notation␈α∞␈↓↓x↑y␈↓␈α∂when␈α∞it␈α∞isn't.␈α∞ Of␈α∞course,␈α∞numerical␈α∞and␈α∂symbolic␈α∞calculation
␈↓ ↓H␈↓must often be combined, so that the function giving the length of a list can be written
␈↓ ↓H␈↓␈↓ α_␈↓↓length u ← ␈↓αif n ␈↓↓u ␈↓αthen 0 else 1 ␈↓↓+ length ␈↓αd ␈↓↓u␈↓.
␈↓ ↓H␈↓␈↓ α_The␈αinternal␈αnotation␈αfor␈αnumerical␈αfunctions␈αis␈α
that␈αused␈αin␈αthe␈αexamples␈αgiven:␈α(PLUS␈α
X
␈↓ ↓H␈↓Y␈α...␈αZ)␈αfor␈α␈↓↓x+y+...+z␈↓,␈α(TIMES␈αX␈α...␈αZ)␈αfor␈α␈↓↓xy...z␈↓,␈α(MINUS␈αX)␈αfor␈α␈↓↓-x␈↓,␈α(DIFFERENCE␈αX␈αY)␈αfor
␈↓ ↓H␈↓␈↓↓x-y␈↓, (QUOTIENT X Y) for ␈↓↓x/y␈↓, and (POWER X Y) for ␈↓↓x␈↓¬y␈↓.
␈↓ ↓H␈↓␈↓ α_Since␈α∂numbers␈α∂that␈α∂form␈α∂part␈α∂of␈α∂list␈α∂structures␈α∂must␈α∂be␈α∂represented␈α∂by␈α∂pointers␈α∂anyway,
␈↓ ↓H␈↓there␈α⊃is␈α∩room␈α⊃for␈α∩a␈α⊃flag␈α∩distinguishing␈α⊃floating␈α∩point␈α⊃numbers␈α∩and␈α⊃integers.␈α∩ Therefore,␈α⊃the
␈↓ ↓H␈↓arithmetic␈α⊃operations␈α⊂are␈α⊃programmed␈α⊂to␈α⊃treat␈α⊂types␈α⊃dynamically,␈α⊂i.e.␈α⊃a␈α⊂variable␈α⊃may␈α⊃take␈α⊂an
␈↓ ↓H␈↓integer␈αvalue␈αat␈α
one␈αstep␈αof␈αcomputation␈α
and␈αa␈αreal␈αvalue␈α
at␈αanother.␈α The␈α
subroutines␈αrealizing
␈↓ ↓H␈↓the␈αarithmetic␈αfunctions␈αmake␈αthe␈αappropriate␈α
tests␈αand␈αcreate␈αresults␈αof␈αappropriate␈α
types.␈α This
␈↓ ↓H␈↓is␈α⊃slow␈α⊂compared␈α⊃to␈α⊃direct␈α⊂use␈α⊃of␈α⊂the␈α⊃machine's␈α⊃arithmetic␈α⊂instructions,␈α⊃so␈α⊂that␈α⊃LISP␈α⊃can␈α⊂be
␈↓ ↓H␈↓efficiently␈α
used␈α
only␈αwhen␈α
the␈α
numerical␈αcalculations␈α
are␈α
small␈αor␈α
at␈α
least␈αsmall␈α
compared␈α
to␈αthe
␈↓ ↓H␈↓symbolic calculations in a problem.
␈↓ ↓H␈↓␈↓ α_Besides␈α
functions␈αof␈α
numbers␈α
we␈αneed␈α
predicates␈α
on␈αnumbers␈α
and␈α
the␈αusual␈α
=,␈α
<,␈α>,␈α
≤,␈αand␈α
≥
␈↓ ↓H␈↓are␈α∨used␈α∨with␈α∨the␈α∨internal␈α∨names␈α∨EQUAL,␈α∨LESSP,␈α∨GREATERP,␈α LESSEQP,␈α∨and
␈↓ ↓H␈↓GREATEREQP,␈α∂respectively.␈α∂ Not␈α∂all␈α∂are␈α∂implemented␈α∂in␈α∂all␈α∂LISP␈α∂systems,␈α∂but␈α∂of␈α∂course␈α∞the
␈↓ ↓H␈↓remaining␈α∪ones␈α∩can␈α∪be␈α∪defined.␈α∩ Besides␈α∪that,␈α∩the␈α∪predicate␈α∪␈↓↓numberp␈↓␈α∩is␈α∪used␈α∪to␈α∩distinguish
␈↓ ↓H␈↓numbers from other atoms.
␈↓ ↓H␈↓␈↓ α_It␈α_is␈α→worth␈α_remarking␈α→that␈α_including␈α_type␈α→flags␈α_in␈α→numbers␈α_would␈α→benefit␈α_many
␈↓ ↓H␈↓programming languages besides LISP and would not cost much in either storage or hardware.
␈↓ ↓H␈↓␈↓ α_As␈αa␈αfirst␈αexample␈αof␈αa␈αcombined␈αnumeric␈αand␈αsymbolic␈αcomputation,␈αhere␈αis␈αan␈αinterpreter
␈↓ ↓H␈↓for␈α∞expressions␈α∂with␈α∞sums␈α∂and␈α∞products.␈α∂ Assume␈α∞that␈α∂the␈α∞values␈α∂of␈α∞variables␈α∂are␈α∞given␈α∂in␈α∞an
␈↓ ↓H␈↓␈↓↓association list␈↓, having the form
␈↓ ↓H␈↓(<variable1>.<value1>) ... (<variablen>.<valuen>)),
␈↓ ↓H␈↓e.g. ((X . 5) (Y . 9.3) (Z . 2.1)).
␈↓ ↓H␈↓␈↓ α_The function is
␈↓ ↓H␈↓␈↓ α_␈↓↓numval[e,a]␈α←␈α␈↓αif␈α␈↓↓numberp␈αe␈α␈↓αthen␈α␈↓↓e␈α␈↓αelse␈αif␈αat␈α␈↓↓e␈α␈↓αthen␈αd␈α␈↓↓assoc[e,a]␈α␈↓αelse␈αif␈αa␈α␈↓↓e␈α␈↓αeq␈αPLUS␈αthen
␈↓ ↓H␈↓α␈↓↓evplus[␈↓αd ␈↓↓e,a] ␈↓αelse if a ␈↓↓e ␈↓αeq TIMES then ␈↓↓evtimes[␈↓αd ␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ ¬oCHAPTER I␈↓ *24
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓evplus[u,a] ← ␈↓αif n ␈↓↓u ␈↓α then 0 else ␈↓↓numval[␈↓αa ␈↓↓u,a] + evplus[␈↓αd ␈↓↓u,a]␈↓,
␈↓ ↓H␈↓␈↓ α_␈↓↓evtimes[u,a] ← ␈↓αif n ␈↓↓u ␈↓α then 1 else ␈↓↓numval[␈↓αa ␈↓↓u,a] + evtimes[␈↓αd ␈↓↓u,a]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓assoc[x,a] ← ␈↓αif n ␈↓↓a ␈↓α then NIL else if aa ␈↓↓a ␈↓αeq ␈↓↓x ␈↓αthen da ␈↓↓a ␈↓αelse ␈↓↓assoc[x,␈↓αd ␈↓↓a]␈↓.
␈↓ ↓H␈↓␈↓ εH␈↓ *25
␈↓ ↓H␈↓α␈↓ εChapter II
␈↓ ↓H␈↓α␈↓ β&HOW TO WRITE RECURSIVE FUNCTION DEFINITIONS
␈↓ ↓H␈↓1. ␈↓αStatic and dynamic ways of programming.␈↓
␈↓ ↓H␈↓␈↓ α_In␈α∩ order␈α∩ to␈α∩ write␈α∩recursive␈α⊃function␈α∩definitions,␈α∩one␈α∩must␈α∩think␈α∩about␈α⊃programming
␈↓ ↓H␈↓differently␈αthan␈αis␈α customary␈α when␈α writing␈αprograms␈α in␈α languages␈αlike␈αFortran␈αor␈αAlgol␈αor␈αin
␈↓ ↓H␈↓machine␈α∪language.␈α∪ In␈α∪these␈α∪languages,␈α∪one␈α∪has␈α∪in␈α∪mind␈α∪the␈α∪state␈α∪of␈α∪the␈α∀ computation␈α∪ as
␈↓ ↓H␈↓represented␈α by␈α
the␈α values␈α
of␈αcertain␈αvariables␈α
or␈αlocations␈α
in␈αthe␈αmemory␈α
of␈αthe␈α
machine,␈αand
␈↓ ↓H␈↓then␈α∞ one␈α∞ writes␈α∞ statements␈α∞ or␈α
machine␈α∞instructions␈α∞in␈α∞order␈α∞to␈α
make␈α∞the␈α∞state␈α∞change␈α∞in␈α
an
␈↓ ↓H␈↓appropriate way.
␈↓ ↓H␈↓␈↓ α_When␈α∞writing␈α
LISP␈α∞recursive␈α∞functions␈α
one␈α∞thinks␈α∞differently.␈α
Namely,␈α∞one␈α∞thinks␈α
about
␈↓ ↓H␈↓the␈αvalue␈αof␈αthe␈α function,␈α asks␈α for␈α what␈αvalues␈α of␈αthe␈αarguments␈αthe␈αvalue␈αof␈αthe␈αfunction␈αis
␈↓ ↓H␈↓immediate,␈α∞and,␈α∞given␈α
an␈α∞ arbitrary␈α∞ values␈α
of␈α∞ the␈α∞ arguments,␈α
for␈α∞ what␈α∞ simpler␈α
arguments
␈↓ ↓H␈↓must␈α∪ the␈α∀ function␈α∪be␈α∀known␈α∪in␈α∀order␈α∪to␈α∪give␈α∀the␈α∪value␈α∀of␈α∪the␈α∀function␈α∪for␈α∀ the␈α∪ given
␈↓ ↓H␈↓arguments.␈α⊃ Let␈α⊃ us␈α⊃ take␈α⊂ a␈α⊃ numerical␈α⊃example;␈α⊃ namely,␈α⊂ suppose␈α⊃ we␈α⊃want␈α⊃to␈α⊃compute␈α⊂the
␈↓ ↓H␈↓function␈α ␈↓↓n!␈↓.␈α For␈αwhat␈αargument␈αis␈αthe␈αvalue␈αof␈αthe␈αfunction␈αimmediate.␈α Clearly,␈α for␈α␈↓↓n␈α=␈α0␈α or
␈↓ ↓H␈↓↓n␈α=␈α1␈↓,␈αthe␈αvalue␈αis␈αimmediately␈αseen␈αto␈αbe␈α 1.␈α Moreover,␈αwe␈αcan␈αget␈αthe␈αvalue␈αfor␈αan␈αarbitrary␈α ␈↓↓n␈↓
␈↓ ↓H␈↓if␈αwe␈αknow␈α the␈α value␈α for␈α␈↓↓n-1.␈α Also,␈αwe␈αsee␈αthat␈αknowing␈αthe␈αvalue␈αfor␈α n␈α=␈α1␈α is␈αredundant,
␈↓ ↓H␈↓↓since␈αit␈αcan␈αbe␈α
obtained␈αfrom␈αthe␈α n␈α
=␈α0␈α case␈αby␈αthe␈α
same␈α rule␈α as␈αgets␈α
it␈α for␈α a␈α general␈α n␈α
from
␈↓ ↓H␈↓↓the value for n-1␈↓. All this talk leads to the simple recursive formula:
␈↓ ↓H␈↓␈↓ α_␈↓↓n! ← ␈↓αif ␈↓↓n = 0 ␈↓αthen 1 else ␈↓↓n(n-1)!.
␈↓ ↓H␈↓␈↓ α_We␈αmay␈αregard␈αthis␈αas␈αa␈αstatic␈αway␈αof␈αlooking␈αat␈αprogramming.␈α We␈αask␈αwhat␈αsimpler␈α
cases
␈↓ ↓H␈↓the␈αgeneral␈αcase␈αof␈αour␈αfunction␈αdepends␈αon␈αrather␈α than␈α how␈α we␈α build␈αup␈αthe␈αdesired␈αstate␈αof
␈↓ ↓H␈↓the␈α∞computation.␈α
One␈α∞often␈α
is␈α∞led␈α∞to␈α
believe␈α∞that␈α
static␈α∞=␈α∞bad␈α
and␈α∞ dynamic␈α
=␈α∞good,␈α∞but␈α
in
␈↓ ↓H␈↓this␈α∩ case,␈α⊃ the␈α∩static␈α⊃way␈α∩is␈α⊃often␈α∩better␈α⊃than␈α∩the␈α⊃dynamic␈α∩way.␈α⊃ Perhaps␈α∩this␈α∩distinction␈α⊃is
␈↓ ↓H␈↓equivalent␈α~to␈α~what␈α≠some␈α~people␈α~call␈α~the␈α≠distinction␈α~between␈α~␈↓↓top-down␈↓␈α≠and␈α~␈↓↓bottom-up␈↓
␈↓ ↓H␈↓programming␈α⊂with␈α⊂␈↓↓static␈↓␈α⊂corresponding␈α⊂to␈α⊂␈↓↓top-down␈↓.␈α⊂ LISP␈α⊂offers␈α⊂both,␈α⊂but␈α⊂the␈α⊂static␈α⊂style␈α∂is
␈↓ ↓H␈↓better developed in LISP, and we will emphasize it.
␈↓ ↓H␈↓␈↓ α_Compare␈α
the␈α∞ above␈α
recursive␈α∞ definition␈α
with␈α∞the␈α
following␈α∞obvious␈α
Algol␈α∞program␈α
for
␈↓ ↓H␈↓computing ␈↓↓n!:
␈↓ ↓H␈↓␈↓ α_␈↓αinteger procedure ␈↓↓factorial(n); ␈↓αinteger ␈↓↓s;
␈↓ ↓H␈↓␈↓αbegin
␈↓ ↓H␈↓α␈↓ α_␈↓↓s := 1;
␈↓ ↓H␈↓α␈↓↓loop: ␈↓αif ␈↓↓n = 0 ␈↓αthen go to ␈↓↓done;
␈↓ ↓H␈↓↓␈↓ α_s := n*s;
␈↓ ↓H␈↓↓␈↓ α_n := n-1;
␈↓ ↓H␈↓↓␈↓ α_␈↓αgo to ␈↓↓loop;
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *26
␈↓ ↓H␈↓↓done: factorial := s;
␈↓ ↓H␈↓↓␈↓αend;
␈↓ ↓H␈↓α␈↓ α_␈↓The␈α∞ LISP␈α∞program␈α∞is␈α∞shorter␈α∂and␈α∞clearer␈α∞in␈α∞this␈α∞particularly␈α∞favorable␈α∂ case.␈α∞ Actually,
␈↓ ↓H␈↓when␈α we␈α discuss␈α the␈α mechanism␈α of␈αrecursion,␈αit␈αwill␈αturn␈αout␈αthat␈αthe␈αLISP␈αprogram␈αwill␈αbe
␈↓ ↓H␈↓inefficient␈α⊃in␈α⊃using␈α∩the␈α⊃pushdown␈α⊃mechanism␈α⊃unnecessarily␈α∩and␈α⊃should␈α⊃be␈α⊃ replaced␈α∩by␈α⊃ the
␈↓ ↓H␈↓following␈α∪ somewhat␈α∪ longer␈α∀ program␈α∪that␈α∪corresponds␈α∪to␈α∀the␈α∪above␈α∪Algol␈α∀program␈α∪rather
␈↓ ↓H␈↓precisely:
␈↓ ↓H␈↓α␈↓ α_␈↓↓n! ← fact(n, s),
␈↓ ↓H␈↓α␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓fact(n, s) ← ␈↓αif ␈↓↓n = 0 ␈↓αthen ␈↓↓s ␈↓αelse ␈↓↓fact(n-1, n*s).
␈↓ ↓H␈↓In fact, compilers should produce the same object code from the two programs.
␈↓ ↓H␈↓2. ␈↓αSimple list recursion.␈↓
␈↓ ↓H␈↓␈↓ α_About␈αthe␈αsimplest␈α
form␈αof␈αrecursion␈αin␈α
LISP␈αoccurs␈αwhen␈α one␈α
of␈αthe␈αarguments␈αis␈α
a␈αlist,
␈↓ ↓H␈↓the␈α
result␈α
is␈αimmediate␈α
when␈α
the␈αargument␈α
is␈α
null,␈α
and␈αotherwise␈α
we␈α
need␈αonly␈α
know␈α
the␈αresult␈α
for
␈↓ ↓H␈↓the␈α
␈↓αd␈↓-part␈αof␈α
that␈αargument.␈α
Consider,␈α
for␈αexample,␈α
␈↓↓u*v␈↓,␈αthe␈α
result␈α
of␈α␈↓↓append␈↓ing␈α
the␈αlist␈α
␈↓↓v␈↓␈αto␈α
the
␈↓ ↓H␈↓list␈α␈↓↓u␈↓.␈α The␈αresult␈α
is␈αimmediate␈αfor␈α the␈α case␈α
␈↓αn␈α␈↓↓u␈↓␈α and␈αotherwise␈αdepends␈α
on␈αthe␈αresult␈αfor␈α ␈↓αd␈α
␈↓↓u␈↓.
␈↓ ↓H␈↓Thus, we have
␈↓ ↓H␈↓␈↓ α_␈↓↓u*v ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓v ␈↓αelse a ␈↓↓u . [␈↓αd ␈↓↓u * v].
␈↓ ↓H␈↓On␈α
the␈α
other␈α
hand,␈α∞if␈α
we␈α
had␈α
tried␈α
to␈α∞recur␈α
on␈α
␈↓↓v␈↓␈α
rather␈α
than␈α∞on␈α
␈↓↓u␈↓␈α
we␈α
would␈α
not␈α∞have␈α
been
␈↓ ↓H␈↓successful.␈α The␈αresult␈αwould␈αbe␈αimmediate␈αfor␈α␈↓αn␈α␈↓↓v␈↓,␈αbut␈α ␈↓↓u*v␈↓␈α cannot␈αbe␈αconstructed␈αin␈αany␈αdirect
␈↓ ↓H␈↓way␈α
from␈α∞ ␈↓↓u*␈↓αd␈α
␈↓↓v␈↓␈α
without␈α∞ a␈α
function␈α
that␈α∞ puts␈α
an␈α
element␈α∞onto␈α
the␈α
end␈α∞of␈α
a␈α
list.␈α∞ (From␈α
a
␈↓ ↓H␈↓strictly␈α∞list␈α∞point␈α∞of␈α∞view,␈α∞such␈α∞ a␈α∂ function␈α∞ would␈α∞ be␈α∞ as␈α∞elementary␈α∞ as␈α∞ ␈↓↓cons␈↓␈α∞ which␈α∂puts␈α∞an
␈↓ ↓H␈↓element␈α
onto␈α
the␈α
front␈α
of␈α
a␈α
list,␈α
but,␈α
when␈α
we␈α
consider␈α
the␈α
implementation␈α
of␈α
lists␈α
by␈αlist␈α
structures,
␈↓ ↓H␈↓we␈α see␈α that␈α the␈α function␈α
is␈αnot␈αso␈αelementary.␈α This␈αhas␈α
led␈αsome␈αpeople␈αto␈αconstruct␈αsystems␈α
in
␈↓ ↓H␈↓which␈α
lists␈α
are␈α
bi-directional,␈α
but,␈α
in␈α
the␈α
main,␈α
this␈α
has␈α
turned␈α
out␈α
to␈α
be␈α
a␈α
bad␈αidea).␈α
Anyway,
␈↓ ↓H␈↓it␈α∂is␈α∂usually␈α∞easier␈α∂to␈α∂recur␈α∞on␈α∂one␈α∂argument␈α∞of␈α∂a␈α∂function␈α∞than␈α∂ to␈α∂ recur␈α∞on␈α∂the␈α∂other.␈α∞ The
␈↓ ↓H␈↓␈↓↓append␈↓␈α
function␈αmachine␈α
coded␈αin␈α
most␈αLisp␈α
systems␈α
in␈αa␈α
way␈αthat␈α
allows␈αan␈α
arbitrary␈αnumber␈α
of
␈↓ ↓H␈↓arguments,␈αe.g.␈α
(APPEND␈α(QUOTE␈α
(A␈αB))␈α(QUOTE␈α
(C␈αD))␈α
(QUOTE␈α(E␈αF)))␈α
is␈α(A␈α
B␈αC␈α
D␈αE
␈↓ ↓H␈↓F).
␈↓ ↓H␈↓␈↓ α_It␈α
is␈α often␈α
necessary␈αto␈α
represent␈αa␈α
correspondence␈αbetween␈α
the␈αelements␈α
of␈αa␈α
small␈α
set␈αof
␈↓ ↓H␈↓atoms␈αand␈αcertain␈α S-expressions␈α by␈α a␈αlist␈αstructure.␈α This␈αis␈αconveniently␈αdone␈αby␈αmeans␈αof␈αan
␈↓ ↓H␈↓association␈α
list␈α
which␈α
is␈α
a␈α
list␈α
of␈α
pairs,␈α
each␈α
pair␈α
consisting␈α
of␈α
an␈α
atom␈α
and␈α
the␈α
corresponding␈α
S-
␈↓ ↓H␈↓expression. Thus the association list
␈↓ ↓H␈↓␈↓ α_((X . (PLUS A B)) (Y . C) (Z . (TIMES U V)),
␈↓ ↓H␈↓which would print as
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *27
␈↓ ↓H␈↓␈↓ α_((X PLUS A B)) (Y . C) (Z TIMES U V)),
␈↓ ↓H␈↓pairs␈α X␈α with␈α (PLUS␈αA␈αB),␈α Y␈α with␈α C,␈αetc.␈αWe␈αneed␈αa␈α function␈α to␈αtell␈α whether␈α anything␈α is
␈↓ ↓H␈↓associated with the atom ␈↓↓x␈↓ in the association list ␈↓↓a␈↓, and, if so, to tell us what. We have
␈↓ ↓H␈↓␈↓ α_␈↓↓assoc[x, a] ← ␈↓αif n ␈↓↓a ␈↓αthen NIL else if ␈↓↓x ␈↓αeq aa ␈↓↓a ␈↓αthen a ␈↓↓a␈↓αelse ␈↓↓assoc[x, ␈↓αd ␈↓↓a].
␈↓ ↓H␈↓Its␈αvalue␈αis␈α NIL␈α if␈α nothing␈α is␈α associated␈α with␈α ␈↓↓x␈↓␈α and␈α the␈αassociation␈αpair␈αotherwise.␈α E.g.
␈↓ ↓H␈↓␈↓↓assoc[X, ((X.W) (Y.V))] = (X.W)␈↓. ␈↓↓assoc␈↓ is also built in to most Lisp systems.
␈↓ ↓H␈↓␈↓ α_It␈α∩ commonly␈α∩happens␈α∩that␈α∩a␈α∩function␈α∩has␈α∩no␈α∩simple␈α∩recursion,␈α∩but␈α∩there␈α∩is␈α∪a␈α∩simple
␈↓ ↓H␈↓recursion␈α
for␈α
a␈α
function␈α
with␈αone␈α
more␈α
variable␈α
that␈α
reduces␈α to␈α
the␈α
desired␈α
function␈α
when␈αthe
␈↓ ↓H␈↓extra variable is set to NIL. Thus
␈↓ ↓H␈↓␈↓ α_␈↓↓reverse[u] ← rev1[x, NIL],
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓rev1[u, v] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓v ␈↓αelse ␈↓↓rev1[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u . v].
␈↓ ↓H␈↓␈↓↓reverse␈↓␈α
has␈αa␈α
direct␈αrecursive␈α
definition␈αas␈α
discovered␈α
by␈αS.␈α
Ness,␈αbut␈α
no-one␈αwould␈α
want␈α
to␈αuse
␈↓ ↓H␈↓the␈αfollowing␈αin␈αactual␈αcomputation␈α nor␈αdoes␈α it␈αgenerate␈αmuch␈αunderstanding,␈αonly␈αappreciation
␈↓ ↓H␈↓of Mr. Ness's ingenuity:
␈↓ ↓H␈↓␈↓ α_␈↓↓reverse[u] ← ␈↓αif n ␈↓↓u ∨ ␈↓αn d ␈↓↓u ␈↓αthen ␈↓↓u ␈↓αelse
␈↓ ↓H␈↓ a ␈↓↓reverse[␈↓αd ␈↓↓u] . reverse[␈↓αa ␈↓↓u. reverse[␈↓αd ␈↓↓reverse[␈↓αd ␈↓↓u]]].
␈↓ ↓H␈↓ Exercises
␈↓ ↓H␈↓␈↓ α_1.␈αUsing␈αthe␈αfunction␈α ␈↓↓member[x,␈αu]␈↓␈α defined␈αin␈αChapter␈αI␈αwhich␈α may␈αalso␈αbe␈αwritten␈α ␈↓↓x␈αε
␈↓ ↓H␈↓↓u␈↓,␈αwrite␈αfunction␈αdefinitions␈αfor␈αthe␈αunion␈α ␈↓↓u␈α∪␈αv␈↓␈α of␈αlists␈α ␈↓↓u␈↓␈α and␈α ␈↓↓v␈↓,␈αthe␈αintersection␈α ␈↓↓u␈α∩␈αv␈↓,␈α and
␈↓ ↓H␈↓the set difference ␈↓↓u-v␈↓. What is wanted should be clear from the examples:
␈↓ ↓H␈↓␈↓ α_(A B C) ∪ (B C D) = (A B C D),
␈↓ ↓H␈↓␈↓ α_(A B C) ∩ (B C D) = (B C),
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_(A B C) - (B C D) = (A).
␈↓ ↓H␈↓Pay␈α∞ attention␈α
to␈α∞getting␈α
correct␈α∞the␈α
trivial␈α∞cases␈α
in␈α∞which␈α
some␈α∞of␈α
the␈α∞arguments␈α
are␈α∞NIL.␈α
In
␈↓ ↓H␈↓general, it is important to understand clearly the trivial cases of functions.
␈↓ ↓H␈↓␈↓ α_2.␈α∂ Suppose␈α∂ ␈↓↓x␈↓␈α∂ takes␈α∂ numbers␈α∂ as␈α∂ values␈α⊂and␈α∂ ␈↓↓u␈↓␈α∂ takes␈α∂as␈α∂values␈α∂lists␈α∂of␈α⊂numbers␈α∂in
␈↓ ↓H␈↓ascending␈α
order,␈αe.g.␈α
(2␈α
4␈α7).␈α
Write␈α a␈α
function␈α
␈↓↓merge[x,␈αu]␈↓␈α
whose␈α value␈α
is␈α
obtained␈αfrom
␈↓ ↓H␈↓that␈α
of␈α
␈↓↓u␈↓␈α
by␈αputting␈α
␈↓↓x␈↓␈α
in␈α
␈↓↓u␈↓␈α in␈α
its␈α
proper␈α
place.␈α
Thus␈α␈↓↓merge[3,␈α
(2␈α
4)]␈α
=␈α(2␈α
3
␈↓ ↓H␈↓↓4)␈↓, and ␈↓↓merge[3, (2 3)] = (2 3 3)␈↓.
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *28
␈↓ ↓H␈↓␈↓ α_3.␈α
Write␈α
functions␈α∞ giving␈α
the␈α
union,␈α∞intersection,␈α
and␈α
set␈α∞difference␈α
of␈α
ordered␈α∞lists;␈α
the
␈↓ ↓H␈↓result is wanted as an ordered list.
␈↓ ↓H␈↓␈↓ α_Note␈α∞that␈α∞computing␈α∂these␈α∞functions␈α∞of␈α∞unordered␈α∂lists␈α∞ takes␈α∞a␈α∞ number␈α∂ of␈α∞comparisons
␈↓ ↓H␈↓proportional␈αto␈αthe␈αsquare␈αof␈αthe␈αnumber␈αof␈αelements␈αof␈αa␈αtypical␈αlist,␈αwhile␈αfor␈αordered␈αlists,␈α the
␈↓ ↓H␈↓number of comparisons is proportional to the number of elements.
␈↓ ↓H␈↓␈↓ α_4.␈α Using␈α ␈↓↓merge␈↓,␈αwrite␈αa␈αfunction␈α
␈↓↓sort␈↓␈α that␈αtransforms␈αan␈αunordered␈αlist␈αinto␈α
an␈αordered
␈↓ ↓H␈↓list.
␈↓ ↓H␈↓␈↓ α_5.␈α∀Write␈α∀a␈α∀function␈α∀ ␈↓↓goodsort␈↓␈α∀ that␈α∃ sorts␈α∀ a␈α∀ list␈α∀ using␈α∀ a␈α∀number␈α∃ of␈α∀ comparisons
␈↓ ↓H␈↓proportional to ␈↓↓n log n␈↓, where ␈↓↓n␈↓ is the length of the list to be sorted.
␈↓ ↓H␈↓3. ␈↓αSimple S-expression recursion.␈↓
␈↓ ↓H␈↓␈↓ α_In␈αanother␈αclass␈αof␈αproblems,␈αthe␈αvalue␈αof␈α the␈α function␈α is␈αimmediate␈α for␈α atomic␈α
symbols,
␈↓ ↓H␈↓and␈α
for␈α
non␈α
atoms␈α
depends␈α
only␈α
on␈α
the␈α
values␈α
for␈α
the␈α
a-part␈α
and␈α
the␈α
d-part␈α
of␈α
the␈α
argument.
␈↓ ↓H␈↓Thus ␈↓↓subst␈↓ was defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓subst[x, y, z] ← ␈↓αif at ␈↓↓z ␈↓αthen [if ␈↓↓z ␈↓αeq ␈↓↓y ␈↓αthen ␈↓↓x ␈↓αelse ␈↓↓z]
␈↓ ↓H␈↓ ␈↓αelse ␈↓↓subst[x, y, ␈↓αa ␈↓↓z] . subst[x , y , ␈↓αd ␈↓↓z].
␈↓ ↓H␈↓↓␈↓ α_␈↓Two␈α⊂other␈α∂examples␈α⊂are␈α∂ ␈↓↓equal␈↓␈α⊂ which␈α⊂gives␈α∂ the␈α⊂ equality␈α∂ of␈α⊂S-expressions␈α⊂ and␈α∂ ␈↓↓flat␈↓
␈↓ ↓H␈↓which spreads an S-expression into a list of atoms: They are defined by
␈↓ ↓H␈↓↓␈↓ α_x=y ← x ␈↓αeq ␈↓↓y ∨ [¬␈↓αat ␈↓↓x ∧ ¬␈↓αat ␈↓↓y ∧ ␈↓αa ␈↓↓x = ␈↓αa ␈↓↓y ∧ ␈↓αd ␈↓↓x = ␈↓αd ␈↓↓y],
␈↓ ↓H␈↓↓␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓flat[x] ← flata[x, NIL]
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓flata[x,u] ← ␈↓αif at ␈↓↓x ␈↓αthen ␈↓↓x.y ␈↓αelse ␈↓↓flata[␈↓αa ␈↓↓x,flata[␈↓αd ␈↓↓x,y]]␈↓.
␈↓ ↓H␈↓The kind of double recursion used in ␈↓↓flat␈↓ is often useful. ␈↓↓flat␈↓ could also be written
␈↓ ↓H␈↓␈↓ α_␈↓↓flat x ← ␈↓αif at ␈↓↓ x ␈↓αthen␈↓↓ <x> ␈↓αthen␈↓↓ flat ␈↓αa␈↓↓ x * flat ␈↓αd␈↓↓ x,
␈↓ ↓H␈↓which␈α∪is␈α∀a␈α∪bit␈α∪easier␈α∀to␈α∪follow␈α∪although␈α∀less␈α∪efficient,␈α∪because␈α∀the␈α∪␈↓↓append␈↓␈α∀function␈α∪copies
␈↓ ↓H␈↓unnecessarily.
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *29
␈↓ ↓H␈↓ EXERCISES
␈↓ ↓H␈↓␈↓ α_1.␈α⊃Write␈α⊃a␈α⊃predicate␈α⊃to␈α⊃tell␈α⊃whether␈α⊃a␈α⊃given␈α⊃atom␈α⊃occurs␈α⊃in␈α⊃a␈α⊃given␈α⊃S-expression,␈α⊃e.g.
␈↓ ↓H␈↓␈↓↓occur[B, ((A.B).C)] = T␈↓.
␈↓ ↓H␈↓␈↓ α_2. Write a predicate to tell how many times a given atom occurs in an S-expression.
␈↓ ↓H␈↓␈↓ α_3.␈α
Write␈α
a␈α
function␈α
to␈αmake␈α
a␈α
list␈α
without␈α
duplications␈αof␈α
the␈α
atoms␈α
occurring␈α
in␈α
an␈αS-
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓␈↓ α_4.␈α∂Write␈α∂a␈α∂function␈α∂to␈α⊂make␈α∂a␈α∂list␈α∂of␈α∂all␈α∂ atoms␈α⊂ that␈α∂ occur␈α∂more␈α∂ than␈α∂ once␈α⊂ in␈α∂ a
␈↓ ↓H␈↓given S-expression paired with their multiplicities.
␈↓ ↓H␈↓␈↓ α_5.␈α∂Write␈α∂a␈α∞predicate␈α∂to␈α∂tell␈α∂whether␈α∞an␈α∂S-expression␈α∂has␈α∂more␈α∞than␈α∂one␈α∂occurrence␈α∂of␈α∞a
␈↓ ↓H␈↓given S-expression as a sub-expression.
␈↓ ↓H␈↓4. ␈↓αOther structural recursions.␈↓
␈↓ ↓H␈↓␈↓ α_When␈α⊗lists␈α⊗are␈α⊗used␈α⊗to␈α⊗represent␈α⊗algebraic␈α⊗expressions,␈α⊗functions␈α⊗of␈α⊗these␈α⊗algebraic
␈↓ ↓H␈↓expressions␈α∃often␈α∃have␈α∀a␈α∃recursive␈α∃form␈α∀closely␈α∃related␈α∃to␈α∀the␈α∃inductive␈α∃definition␈α∃of␈α∀the
␈↓ ↓H␈↓expressions.␈α∂ Suppose,␈α∞for␈α∂example,␈α∞that␈α∂sums␈α∂and␈α∞products␈α∂are␈α∞represented␈α∂respectively␈α∂by␈α∞the
␈↓ ↓H␈↓forms␈α (PLUS␈αX␈αY)␈α and␈α (TIMES␈αX␈αY)␈α and␈α
that␈αthe␈αvalues␈αof␈αvariables␈αare␈αgiven␈αon␈αan␈α
a-list.
␈↓ ↓H␈↓We can write a recursive formula for the value of an expression as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓value[e, a] ← ␈↓αif at ␈↓↓e ␈↓αthen d ␈↓↓assoc[e, a]
␈↓ ↓H␈↓ ␈↓αelse if a ␈↓↓e ␈↓αeq PLUS then ␈↓↓value[␈↓αad ␈↓↓e, a] + value[␈↓αadd ␈↓↓e, a]
␈↓ ↓H␈↓↓ ␈↓αelse if a ␈↓↓e ␈↓αeq TIMES then ␈↓↓value[␈↓αad ␈↓↓e, a] * value[␈↓αadd ␈↓↓e, a].
␈↓ ↓H␈↓↓␈↓On␈α
the␈αother␈α
hand,␈α
suppose␈αthat␈α
sums␈αand␈α
products␈α
are␈αnot␈α
restricted␈α
to␈αhave␈α
just␈αtwo␈α
arguments;
␈↓ ↓H␈↓then we must use auxiliary functions to define the value of an expression, as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓value[e, a] ← ␈↓αif at ␈↓↓e ␈↓αthen d ␈↓↓assoc[e, a]
␈↓ ↓H␈↓ ␈↓αelse if a ␈↓↓e ␈↓αeq PLUS then ␈↓↓vplus[␈↓αd ␈↓↓e, a]
␈↓ ↓H␈↓↓ ␈↓αelse if a ␈↓↓e ␈↓αeq TIMES then ␈↓↓vtimes[␈↓αd ␈↓↓e, a].
␈↓ ↓H␈↓↓␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓vplus[u, a] ← ␈↓αif n ␈↓↓u ␈↓αthen 0 else ␈↓↓value[␈↓αa ␈↓↓u, a] + vplus[␈↓αd ␈↓↓u, a],
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓vtimes[u, a] ← ␈↓αif n ␈↓↓u ␈↓αthen 1 else ␈↓↓value[␈↓αa ␈↓↓u, a] * vtimes[␈↓αd ␈↓↓u, a].
␈↓ ↓H␈↓In␈α∂both␈α⊂cases,␈α∂the␈α∂recursion␈α⊂form␈α∂is␈α∂related␈α⊂to␈α∂the␈α∂structure␈α⊂of␈α∂the␈α∂algebraic␈α⊂expressions␈α∂more
␈↓ ↓H␈↓closely than to the structure of S-expressions or lists.
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *30
␈↓ ↓H␈↓5. ␈↓αTree search recursion.␈↓
␈↓ ↓H␈↓␈↓ α_We␈αbegin␈αwith␈αa␈αgeneral␈αdepth␈αfirst␈αtree␈αsearch␈αfunction.␈α It␈αcan␈αbe␈αused␈αto␈αsearch␈αspecific
␈↓ ↓H␈↓trees␈α∃of␈α∃possibilities␈α∃by␈α∀defining␈α∃three␈α∃auxiliary␈α∃functions␈α∃in␈α∀a␈α∃way␈α∃that␈α∃depends␈α∃on␈α∀the
␈↓ ↓H␈↓application. We have
␈↓ ↓H␈↓␈↓ α_␈↓↓search p ← ␈↓αif ␈↓↓lose p ␈↓αthen ␈↓LOSE
␈↓ ↓H␈↓ ␈↓αelse if ␈↓↓ter p ␈↓αthen ␈↓↓p ␈↓αelse ␈↓↓searchlis[␈↓successors p]
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓searchlis␈αu␈α←␈α␈↓α␈αif␈αn␈α␈↓↓u␈α␈↓αthen␈α␈↓LOSE␈α␈↓αelse␈α␈↓↓{␈↓search␈α␈↓αa␈α ␈↓↓u}[␈↓λx:␈α␈↓αif␈α␈↓↓x␈α=␈α␈↓LOSE␈α␈↓αthen␈α␈↓↓searchlis␈α␈↓αd␈α␈↓↓u␈α␈↓αelse
␈↓ ↓H␈↓α␈↓↓x]␈↓.
␈↓ ↓H␈↓In␈αthe␈α
applications,␈αwe␈α start␈α
with␈αa␈α
position␈α␈↓↓p␈↓∧0␈↓,␈α and␈α
we␈αare␈α
looking␈αfor␈α a␈α
win␈αin␈α
the␈αsuccessor
␈↓ ↓H␈↓tree␈α∞of␈α∞␈↓↓p␈↓∧0␈↓.␈α∂ Certain␈α∞positions␈α∞lose␈α∞and␈α∂ there␈α∞is␈α∞ no␈α∞point␈α∂looking␈α∞ at␈α∞their␈α∞ successors.␈α∂ This␈α∞is
␈↓ ↓H␈↓decided␈α⊂ by␈α⊂the␈α⊂predicate␈α⊃␈↓↓lose␈↓.␈α⊂A␈α⊂position␈α⊂ is␈α⊃a␈α⊂win␈α⊂if␈α⊂it␈α⊃ doesn't␈α⊂ lose␈α⊂ and␈α⊂ it␈α⊃ satisfies␈α⊂ the
␈↓ ↓H␈↓predicate␈α
␈↓↓ter␈↓.␈α
The␈α
successors␈α
of␈α
a␈α
position␈α
is␈α
given␈α
by␈α
the␈α
function␈α
␈↓↓successors␈↓,␈α
and␈α
the␈α value
␈↓ ↓H␈↓of␈α␈↓↓search␈α
p␈↓␈α is␈α
the␈α winning␈α
position.␈α No␈α
non-losing␈αposition␈α
should␈αhave␈α
the␈α name␈αLOSE␈α
or
␈↓ ↓H␈↓the function won't work properly.
␈↓ ↓H␈↓␈↓ α_Simple␈αS-expression␈αrecursion␈αcan␈αbe␈αregarded␈αas␈αa␈αspecial␈αcase␈αof␈αdepth␈αfirst␈αrecursion.␈α It
␈↓ ↓H␈↓is␈αspecial␈αin␈αthat␈αthere␈αexactly␈α
two␈αbranches,␈αbut␈αeven␈αmore␈αimportant,␈α
the␈αtree␈αis␈αthe␈αtree␈αof␈α
parts
␈↓ ↓H␈↓of␈α∂the␈α∂S-expression␈α∂and␈α∂is␈α∂present␈α∂at␈α⊂the␈α∂beginning␈α∂of␈α∂the␈α∂calculation.␈α∂ In␈α∂case␈α∂of␈α⊂tree␈α∂search
␈↓ ↓H␈↓recursion, the tree is generated by the ␈↓↓successors␈↓ function.
␈↓ ↓H␈↓␈↓ α_Our␈α
first␈α
application␈α
is␈α
finding␈α
a␈α
path␈α
from␈α
an␈α
initial␈α
node␈α
to␈α
a␈α
final␈α
node␈α
in␈α
a␈αgraph
␈↓ ↓H␈↓represented␈αby␈αa␈αlist␈αstructure␈αas␈αdescribed␈αin␈αchapter␈αI.␈α A␈αposition␈αis␈αa␈αpath␈α starting␈αfrom␈αthe
␈↓ ↓H␈↓initial␈α∂ node␈α∂and␈α∂ continuing␈α∂to␈α∂ some␈α∂intermediate␈α∞ node␈α∂and␈α∂ is␈α∂represented␈α∂ by␈α∂a␈α∂list␈α∂ of␈α∞its
␈↓ ↓H␈↓nodes in reverse order. The three functions for this application are
␈↓ ↓H␈↓␈↓ α_␈↓↓lose p ← ␈↓αa ␈↓↓p ε ␈↓αd ␈↓↓p,
␈↓ ↓H␈↓␈↓ α_ter p ← [␈↓αa ␈↓↓p = final],␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓successors p ←␈↓ mapcar[␈↓αd ␈↓↓assoc[␈↓αa ␈↓↓p, graph]␈↓, λx: x.p].
␈↓ ↓H␈↓␈↓ α_Another␈α∞example␈α∞is␈α∞the␈α∞so-called␈α∞␈↓↓Instant␈α∞Insanity␈↓␈α∞puzzle.␈α∞ There␈α∞are␈α∞four␈α∞cubical␈α∞blocks,
␈↓ ↓H␈↓and␈α∞each␈α∞face␈α∞of␈α∞each␈α∞block␈α∞is␈α∞colored␈α∞with␈α
one␈α∞of␈α∞four␈α∞colors.␈α∞ The␈α∞object␈α∞of␈α∞the␈α∞puzzle␈α∞is␈α
to
␈↓ ↓H␈↓build␈αa␈αtower␈αof␈αall␈αfour␈αblocks␈αsuch␈αthat␈αeach␈αvertical␈αface␈αof␈αthe␈αtower␈αinvolves␈αall␈αfour␈αcolors.
␈↓ ↓H␈↓In␈α∃order␈α∃to␈α∀use␈α∃the␈α∃above␈α∃defined␈α∀function␈α∃␈↓↓search␈↓␈α∃for␈α∃this␈α∀purpose,␈α∃we␈α∃must␈α∃define␈α∀the
␈↓ ↓H␈↓representation␈α∪of␈α∪positions␈α∀and␈α∪give␈α∪the␈α∀functions␈α∪␈↓↓lose,␈α∪ter,␈α∀␈↓and␈α∪␈↓↓successors␈↓.␈α∪ A␈α∀position␈α∪is
␈↓ ↓H␈↓represented␈αby␈αa␈αlist␈αof␈αlists␈α¬␈αone␈αfor␈αeach␈αface␈αof␈αthe␈αtower.␈α Each␈αsublist␈αis␈αthe␈αlist␈αof␈αcolors␈αof
␈↓ ↓H␈↓the␈αfaces␈α
of␈αthe␈αblocks␈α
showing␈αin␈α
that␈αface.␈α We␈α
shall␈αassume␈α
that␈αthe␈αblocks␈α
are␈αdescribed␈αin␈α
the
␈↓ ↓H␈↓following␈α∂longwinded␈α∂but␈α∂convenient␈α∂way.␈α∂ (We'll␈α∂take␈α∂up␈α∂precomputing␈α∂this␈α∂description␈α∂later.)
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *31
␈↓ ↓H␈↓For␈α⊃each␈α∩block␈α⊃there␈α∩is␈α⊃a␈α⊃list␈α∩of␈α⊃the␈α∩24␈α⊃orientations␈α⊃of␈α∩the␈α⊃block␈α∩where␈α⊃each␈α∩orientation␈α⊃is
␈↓ ↓H␈↓described␈α∪as␈α∩a␈α∪list␈α∩of␈α∪the␈α∩colors␈α∪around␈α∪the␈α∩vertical␈α∪faces␈α∩of␈α∪the␈α∩block␈α∪when␈α∩it␈α∪is␈α∪in␈α∩that
␈↓ ↓H␈↓orientation. Thus the puzzle is described by a list of lists of lists which we shall call ␈↓↓puzz␈↓.
␈↓ ↓H␈↓␈↓ α_We now have
␈↓ ↓H␈↓␈↓ α_␈↓↓p␈↓∧0␈↓ = (NIL NIL NIL NIL),
␈↓ ↓H␈↓␈↓ α_␈↓↓lose p ←␈↓ orlis[p, λu: ␈↓αa ␈↓↓u ε ␈↓αd ␈↓↓u]␈↓,
␈↓ ↓H␈↓␈↓ α_ter p ← [length ␈↓αa ␈↓↓p =␈↓ 4],
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓successors p ←␈↓ mapcar[␈↓αa ␈↓↓nth[␈↓puzz, 1 + length ␈↓αa ␈↓↓p]␈↓ , λx: mapcar2[p, x, λyz: z.y]],
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓mapcar2[␈↓u, v, f] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓NIL ␈↓αelse f[a ␈↓↓u, ␈↓αa ␈↓↓v]␈↓ . mapcar2[␈↓αd ␈↓↓u, ␈↓αd ␈↓↓v, f]␈↓.
␈↓ ↓H␈↓␈↓ α_Getting␈α
the␈α
initial␈α
position␈αin␈α
the␈α
desired␈α
form␈α
is␈αas␈α
complicated␈α
a␈α
computation␈α
as␈αthe␈α
actual
␈↓ ↓H␈↓tree␈α
search.␈α
It␈α
can␈α
be␈α
conveniently␈α
done␈αby␈α
a␈α
sequence␈α
of␈α
assignment␈α
statements␈α
starting␈α
with␈αa
␈↓ ↓H␈↓description of the blocks:
␈↓ ↓H␈↓␈↓ α_␈↓↓puzz1 ← ␈↓((G B B W R G) (G G B G W R) (G W W R B R) (G G R B W W)).
␈↓ ↓H␈↓Here␈αeach␈αblock␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αthe␈αcolors␈αof␈αthe␈αfaces␈αstarting␈αwith␈αthe␈αtop␈αface,␈αgoing
␈↓ ↓H␈↓around the sides in a clockwise direction and finishing with the bottom face.
␈↓ ↓H␈↓␈↓ α_We␈αneed␈αto␈α
go␈αfrom␈αthis␈α
description␈αof␈αthe␈α
blocks␈αto␈αa␈α
list␈αof␈αthe␈α
possible␈αcycles␈αof␈αcolors␈α
on
␈↓ ↓H␈↓the␈αvertical␈αfaces␈αfor␈αthe␈α24␈αorientations␈αof␈αthe␈αblock.␈α This␈αnot␈αeasy,␈αbecause␈αthe␈αorder␈αin␈αwhich
␈↓ ↓H␈↓we␈α
have␈α
given␈αthe␈α
colors␈α
is␈α
not␈αinvariant␈α
under␈α
rotations␈α
of␈αthe␈α
block.␈α
An␈α
easy␈αway␈α
out␈α
is␈αto␈α
start
␈↓ ↓H␈↓with␈αa␈αblock␈αwhose␈αfaces␈αare␈αassigned␈αthe␈αnumbers␈α1␈αthru␈α6␈αstarting␈αwith␈αthe␈αtop,␈αgoing␈α
clockwise
␈↓ ↓H␈↓around␈αthe␈αsides␈αand␈αfinishing␈αwith␈αthe␈αbottom.␈α We␈αwrite␈αdown␈αone␈αcycle␈αof␈αside␈αcolors␈αfor␈αeach
␈↓ ↓H␈↓choice␈α∩of␈α⊃the␈α∩face␈α⊃put␈α∩on␈α⊃top␈α∩and␈α⊃get␈α∩the␈α⊃list␈α∩of␈α⊃all␈α∩24␈α⊃cycles␈α∩by␈α⊃appending␈α∩the␈α∩results␈α⊃of
␈↓ ↓H␈↓generating the cyclic permutations of the cycles. All this is accomplished by the assignment\.
␈↓ ↓H␈↓␈↓ α_␈↓↓puzz2 ←␈↓ cycles[(2 3 4 5)]*␈↓↓cycles[␈↓((2 5 4 3)]* ␈↓↓cycles[␈↓(1 2 6 4)]
␈↓ ↓H␈↓ *cycles[(1 4 6 2)]*cycles[(1 3 6 5)]*cycles[(1 5 6 3)],
␈↓ ↓H␈↓where the function ␈↓↓cycles␈↓ is defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓cycles u ←␈↓ maplist[u, λx: x*upto[u, x]]
␈↓ ↓H␈↓with the auxiliary function
␈↓ ↓H␈↓␈↓ α_␈↓↓upto[␈↓u, x] ← ␈↓αif ␈↓↓x ␈↓αeq ␈↓↓u ␈↓αthen ␈↓NIL ␈↓αelse a ␈↓↓u . upto[␈↓αd ␈↓↓u, x]␈↓.
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *32
␈↓ ↓H␈↓Next␈α∞we␈α∞create␈α∞for␈α∞each␈α∞block␈α∞a␈α∞list␈α∞of␈α∞substitutions␈α∞expressing␈α∞the␈α∞colors␈α∞of␈α∞the␈α∂six␈α∞numbered
␈↓ ↓H␈↓faces. We have
␈↓ ↓H␈↓␈↓ α_␈↓↓puzz3 ←␈↓ mapcar[puzz1, λx: prup[(1 2 3 4 5 6), ␈↓↓x]], ␈↓
␈↓ ↓H␈↓and we use these substitutions to get for each block the list of 24 orientations of the block. Thus
␈↓ ↓H␈↓␈↓ α_␈↓↓puzz4 ← mapcar[puzz3, λs: sublis[s, puzz3]]␈↓.
␈↓ ↓H␈↓puzz4␈αhas␈αall␈α24␈αorientations␈αof␈αthe␈αfirst␈αblock␈αwhile␈αfor␈αsymmetry␈αreasons␈αwe␈αneed␈αonly␈αconsider
␈↓ ↓H␈↓three as distinct, say the first, ninth, and seventeen. So we finally get
␈↓ ↓H␈↓␈↓ α_␈↓↓puzz ← (␈↓αa ␈↓↓nth[␈↓αa ␈↓↓puzz4, 1] ␈↓αa ␈↓↓nth[␈↓αa ␈↓↓puzz4, 9] ␈↓αa ␈↓↓nth[␈↓αa ␈↓↓puzz4, 17]) . ␈↓αd ␈↓↓puzz4.␈↓
␈↓ ↓H␈↓The program when compiled runs about 11 seconds on the PDP-10.
␈↓ ↓H␈↓␈↓ α_A␈αmore␈αsophisticated␈αrepresentation␈αof␈αface␈α
cycles␈αand␈αpartial␈αtowers␈αmakes␈αa␈αfactor␈α
of␈αten
␈↓ ↓H␈↓speedup␈αwithout␈αchanging␈αthe␈αbasic␈αsearch␈αalgorithm.␈α A␈αface␈αcycle␈αis␈αrepresented␈αby␈α16␈αbits␈αin␈αa
␈↓ ↓H␈↓word␈α
four␈α
for␈α
each␈α
face␈α
a␈α
particular␈α
one␈α
of␈α
which␈α
being␈α
turned␈α
on␈α
tells␈α
us␈α
the␈α
color␈α
of␈α
the␈αface.␈α
If
␈↓ ↓H␈↓we␈α␈↓αor␈↓␈αthese␈αwords␈αtogether␈αfor␈αthe␈αblocks␈αin␈αa␈αpartial␈αtower␈αwe␈αget␈αa␈αword␈αwhich␈αtells␈αus␈αfor␈αeach
␈↓ ↓H␈↓face␈αof␈αthe␈αtower␈αwhat␈αcolors␈αhave␈αbeen␈αused.␈α A␈αparticular␈αface␈αcycle␈αfrom␈αthe␈αnext␈αblock␈αcan␈αbe
␈↓ ↓H␈↓added␈α∂to␈α∂the␈α⊂tower␈α∂if␈α∂the␈α∂␈↓αand␈↓␈α⊂of␈α∂its␈α∂word␈α⊂with␈α∂the␈α∂word␈α∂representing␈α⊂the␈α∂tower␈α∂is␈α⊂zero.␈α∂ We
␈↓ ↓H␈↓represent␈α
a␈α
position␈α
by␈α
a␈α
list␈α
of␈α
words␈α
representing␈α
its␈α
partial␈α
towers␈α
with␈α
0␈α
as␈α
the␈α
last␈αelement␈α
and
␈↓ ↓H␈↓the␈α
highest␈α
partial␈α
tower␈α
as␈α
the␈α
first␈α
element.␈α
The␈α
virtue␈α
of␈α
this␈α
representation␈α
is␈α
that␈α
it␈αmakes
␈↓ ↓H␈↓the␈αdescription␈αof␈αthe␈αalgorithm␈αshort.␈α The␈α
initial␈αposition␈αis␈α(0).␈α The␈αnew␈α␈↓↓puzz␈↓␈αcan␈α
be␈αformed
␈↓ ↓H␈↓from the old one by the assignment
␈↓ ↓H␈↓␈↓ α_␈↓↓puzza ← mapcar[puzz, λx: mapcar[x, zap]], ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓zap v ← ␈↓αif n ␈↓↓v ␈↓αthen 0 else ␈↓↓poo ␈↓αa ␈↓↓v + 16 * zap ␈↓αd ␈↓↓v, ␈↓\.
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓poo x ← ␈↓αif ␈↓↓x=␈↓R ␈↓αthen ␈↓↓1 ␈↓αelse if ␈↓↓x=␈↓W ␈↓αthen ␈↓2 ␈↓αelse if ␈↓↓x=␈↓G ␈↓αthen ␈↓4 ␈↓αelse ␈↓8.
␈↓ ↓H␈↓Now we need the new functions ␈↓↓lose, ter, ␈↓and ␈↓↓successors␈↓. These are
␈↓ ↓H␈↓␈↓ α_␈↓↓lose p ← ␈↓αfalse␈↓↓,
␈↓ ↓H␈↓␈↓ α_␈↓↓ter p ← [length p = 5], ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓successors p ← mapchoose[␈↓αa ␈↓↓nth[puzza, length p], λx: ␈↓αa ␈↓↓p ␈↓αand ␈↓↓x = 0, λx: [␈↓αa ␈↓↓p ␈↓αor ␈↓↓x] . p].␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓mapchoose[u, pred, fn] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓NIL
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *33
␈↓ ↓H␈↓ ␈↓αelse if ␈↓↓pred ␈↓αa ␈↓↓u ␈↓αthen ␈↓↓fn[␈↓αa ␈↓↓u] . mapchoose[␈↓αd ␈↓↓u , pred, fn] ␈↓α
␈↓ ↓H␈↓α else ␈↓↓mapchoose[␈↓αd ␈↓↓u, pred, fn].␈↓
␈↓ ↓H␈↓␈↓↓lose␈↓␈αis␈αtrivial,␈αbecause␈αthe␈α␈↓↓mapchoose␈↓␈αis␈αused␈αto␈αmake␈αsure␈αthat␈αonly␈αnon-losing␈αnew␈αpositions␈αare
␈↓ ↓H␈↓generated␈α∞by␈α∞␈↓↓successors␈↓.␈α∞ This␈α∞version␈α∞runs␈α∞in␈α∂a␈α∞little␈α∞less␈α∞than␈α∞one␈α∞second␈α∞on␈α∞the␈α∂PDP-10.␈α∞ A
␈↓ ↓H␈↓greater␈α⊃speedup␈α⊂can␈α⊃be␈α⊂made␈α⊃by␈α⊃the␈α⊂application␈α⊃of␈α⊂some␈α⊃mathematics.␈α⊂ In␈α⊃fact,␈α⊃with␈α⊂enough
␈↓ ↓H␈↓mathematics, extensive tree search is unnecessary in this problem.
␈↓ ↓H␈↓␈↓ α_␈↓↓search␈↓␈α∞is␈α∞used␈α
when␈α∞we␈α∞want␈α
to␈α∞search␈α∞a␈α∞tree␈α
of␈α∞possibilities␈α∞for␈α
a␈α∞solution␈α∞to␈α∞a␈α
problem.
␈↓ ↓H␈↓Naturally␈α∞we␈α∞can␈α∞do␈α∞other␈α∞things␈α∞with␈α∞tree␈α∞search␈α∞recursion␈α∞than␈α∞just␈α∞search.␈α∞ For␈α∞example␈α
we
␈↓ ↓H␈↓may␈α
want␈αto␈α
find␈αall␈α
solutions␈α
to␈αa␈α
problem.␈α This␈α
can␈αbe␈α
done␈α
with␈αa␈α
function␈α␈↓↓allsol␈↓␈α
that␈αuses␈α
the
␈↓ ↓H␈↓same ␈↓↓lose, ter, ␈↓and ␈↓↓successors␈↓ functions as does ␈↓↓search␈↓. The simplest way to write ␈↓↓allsol␈↓ is
␈↓ ↓H␈↓␈↓ α_␈↓↓allsol p ← ␈↓αif ␈↓↓lose p ␈↓αthen ␈↓NIL ␈↓αelse if ␈↓↓ter p ␈↓αthen ␈↓↓(p) ␈↓αelse ␈↓↓mapapp[successors p, allsol], ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓mapapp[u, fn] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓NIL ␈↓αelse ␈↓↓fn[␈↓αa ␈↓↓u] . mappap[␈↓αd ␈↓↓u, fn].␈↓
␈↓ ↓H␈↓This␈αform␈αof␈αthe␈αfunction␈αis␈αsomewhat␈αinefficient␈αbecause␈αof␈αall␈αthe␈α␈↓↓append␈↓ing.␈α A␈αmore␈αefficient
␈↓ ↓H␈↓form uses an auxiliary function as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓allsol p ← allsola[p, ␈↓NIL]
␈↓ ↓H␈↓␈↓ α_␈↓↓allsola[p, found] ← ␈↓αif ␈↓↓lose p ␈↓αthen ␈↓↓found
␈↓ ↓H␈↓ ␈↓αelse if ␈↓↓ter p ␈↓αthen ␈↓↓p . found
␈↓ ↓H␈↓↓ ␈↓αelse ␈↓↓allsolb[successors p, found],
␈↓ ↓H␈↓↓␈↓ α_allsolb[u, found] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓found ␈↓αelse ␈↓↓allsolb[␈↓αd ␈↓↓u, allsola[␈↓αa ␈↓↓u, found]].␈↓
␈↓ ↓H␈↓↓The␈αrecursive␈αprogram␈αstructure␈αthat␈αarises␈αhere␈αis␈αcommon␈αwhen␈αa␈αlist␈αis␈αto␈αbe␈αformed␈αby␈αrecurring
␈↓ ↓H␈↓↓through a list structure.
␈↓ ↓H␈↓↓6. ␈↓αGame trees.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α
positions␈α
that␈α
can␈α
be␈α
reached␈α
from␈α
an␈α
initial␈α
position␈α
in␈α
a␈α
game␈α
form␈α
a␈α∞ tree,␈α
and
␈↓ ↓H␈↓deciding␈α
what␈αmove␈α
to␈α
make␈αoften␈α
involves␈α
searching␈αthis␈α
tree.␈α
However,␈αgame␈α
trees␈αare␈α
searched
␈↓ ↓H␈↓in␈αa␈αdifferent␈αway␈α than␈αthe␈αtrees␈αwe␈α
have␈αlooked␈αat,␈αbecause␈αthe␈αopposing␈αinterests␈αof␈αthe␈α
players
␈↓ ↓H␈↓makes␈αit␈αnot␈αa␈αsearch␈αfor␈αa␈αjoint␈αline␈α of␈α play␈α that␈αwill␈α lead␈α to␈α the␈α first␈α player␈αwinning,␈αbut
␈↓ ↓H␈↓rather a search for a strategy that will lead to a win regardless of what the other player does.
␈↓ ↓H␈↓␈↓ α_The␈α∃ simplest␈α∃ situation␈α∃ is␈α∃ characterized␈α∃ by␈α∃ a␈α∃ function␈α∃␈↓↓successors␈↓␈α∃that␈α∃gives␈α∃the
␈↓ ↓H␈↓positions␈αthat␈αcan␈αbe␈αreached␈αin␈α one␈αmove,␈α a␈α predicate␈α ␈↓↓ter␈↓␈α that␈α tells␈α when␈αa␈αposition␈αis␈αto␈αbe
␈↓ ↓H␈↓regarded␈α as␈α terminal␈α for␈α the␈α given␈α analysis,␈α and␈α a␈α function␈α␈↓↓imval␈↓␈α that␈α gives␈α a␈α number
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *34
␈↓ ↓H␈↓approximating␈α∞ the␈α∞ value␈α∞ of␈α∞the␈α
position␈α∞to␈α∞one␈α∞of␈α∞the␈α
players.␈α∞ We␈α∞ shall␈α∞ call␈α∞ this␈α
player
␈↓ ↓H␈↓the␈α
maximizing␈α player␈α
and␈α his␈α
opponent␈αthe␈α
minimizing␈αplayer.␈α
Usually,␈αthe␈α
numerical␈αvalues
␈↓ ↓H␈↓arise,␈α
because␈α
the␈α
search␈α
cannot␈α
be␈α
carried␈α
out␈α
to␈α
the␈α
end␈α
of␈α
the␈α
game,␈α
and␈α
the␈α
analysis␈α
stops␈α
with
␈↓ ↓H␈↓reasonably␈α⊃static␈α⊃positions␈α⊃that␈α⊂ can␈α⊃ be␈α⊃ evaluated␈α⊃ by␈α⊂ some␈α⊃ rule.␈α⊃ Naturally,␈α⊃ the␈α⊂function
␈↓ ↓H␈↓␈↓↓imval␈↓␈α∂ is␈α∞ chosen␈α∂ to␈α∞ be␈α∂ easy␈α∞ to␈α∂ calculate␈α∂and␈α∞to␈α∂correlate␈α∞well␈α∂with␈α∞the␈α∂probability␈α∂that␈α∞the
␈↓ ↓H␈↓maximizing player can win the position.
␈↓ ↓H␈↓␈↓ α_The␈αsimplest␈α
rule␈αfor␈α
finding␈αthe␈αcorrect␈α
move␈αin␈α
a␈αposition␈α
uses␈αauxiliary␈αfunctions␈α
␈↓↓valmax␈↓
␈↓ ↓H␈↓and␈α
␈↓↓valmin␈↓␈αthat␈α
give␈αa␈α
value␈αto␈α
a␈α
position␈αby␈α
using␈α␈↓↓imval␈↓␈α
if␈αthe␈α
position␈αis␈α
terminal␈α
and␈αtaking
␈↓ ↓H␈↓the max or min of the successor positions otherwise.
␈↓ ↓H␈↓␈↓ α_For␈α
this␈α
we␈α
want␈α
functions␈α
for␈α
getting␈α
the␈α
maximum␈α
or␈α
the␈α
minimum␈α
of␈α
a␈α
function␈α
on␈α
a␈α
list,
␈↓ ↓H␈↓and they are defined as follows:
␈↓ ↓H␈↓␈↓ α_␈↓↓maxlis[u, f] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓-∞ ␈↓αelse ␈↓↓ max[f[␈↓αa ␈↓↓u], maxlis[␈↓αd ␈↓↓u, f]], ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓minlis[u, f] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓∞ ␈↓αelse ␈↓↓ min[f[␈↓αa ␈↓↓u], minlis[␈↓αd ␈↓↓u, f]].␈↓
␈↓ ↓H␈↓In␈αthese␈αfunctions,␈α
-∞␈αand␈α∞␈α
represent␈αnumbers␈αthat␈α
are␈αsmaller␈αand␈α
larger␈αthan␈αany␈αactual␈α
values
␈↓ ↓H␈↓that␈α
will␈αoccur␈α
in␈αevaluating␈α
␈↓↓f␈↓.␈α If␈α
these␈αnumbers␈α
are␈αnot␈α
available,␈αthen␈α
it␈αis␈α
necessary␈α
to␈αprime
␈↓ ↓H␈↓the␈α∞function␈α∞with␈α∞the␈α∞values␈α∞of␈α∞␈↓↓f␈↓␈α∞applied␈α∞to␈α∞the␈α∞first␈α∞element␈α∞of␈α∞the␈α∞list,␈α∞and␈α∞the␈α∞functions␈α∞are
␈↓ ↓H␈↓meaningless␈αfor␈αnull␈αlists.␈α Iterative␈α
versions␈αof␈αthe␈αfunctions␈αare␈α
generally␈αbetter;␈αwe␈αgive␈αonly␈α
the
␈↓ ↓H␈↓iterative version of ␈↓↓maxlis␈↓, namely
␈↓ ↓H␈↓␈↓ α_␈↓↓maxlis[u, f] ← maxlisa[u, -∞, f], ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ α_␈↓↓maxlisa[u, x, f] ← if n u ␈↓αthen ␈↓↓x ␈↓αelse ␈↓↓maxlisa[␈↓αd ␈↓↓u, max[x, f[␈↓αa ␈↓↓u]], f].␈↓
␈↓ ↓H␈↓We now have
␈↓ ↓H␈↓␈↓ α_␈↓↓valmax p ← ␈↓αif ␈↓↓ter p ␈↓αthen ␈↓↓imval p ␈↓αelse ␈↓↓maxlis[successors p, valmin]␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓valmin p ← ␈↓αif ␈↓↓ter p ␈↓αthen ␈↓↓imval p ␈↓αelse ␈↓↓minlis[successors p, valmax]␈↓.
␈↓ ↓H␈↓The best move is now chosen by
␈↓ ↓H␈↓␈↓ α_␈↓↓movemax p ← bestmax[succesors p, valmin], ␈↓
␈↓ ↓H␈↓or
␈↓ ↓H␈↓␈↓ α_␈↓↓movemin p ← bestmin[succesors p, valmax], ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *35
␈↓ ↓H␈↓␈↓ α_␈↓↓bestmax[u, f] ← bestmaxa[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u, f[␈↓αa ␈↓↓u], f],
␈↓ ↓H␈↓␈↓ α_␈↓↓bestmaxa[u, sofar, valsofar, f] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓sofar
␈↓ ↓H␈↓ ␈↓αelse if ␈↓↓f[␈↓αa ␈↓↓u] ≤ bestsofar ␈↓αthen ␈↓↓bestmaxa[␈↓αd ␈↓↓u, sofar, bestsofar, f]
␈↓ ↓H␈↓↓ ␈↓αelse ␈↓↓bestmaxa[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u, f[␈↓αa ␈↓↓u], f],
␈↓ ↓H␈↓↓␈↓ α_bestmin[u, f] ← bestmina[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u, f[␈↓αa ␈↓↓u], f], ␈↓
␈↓ ↓H␈↓↓and
␈↓ ↓H␈↓↓␈↓ α_bestmina[u, sofar, valsofar, f] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓sofar
␈↓ ↓H␈↓↓ ␈↓αelse if ␈↓↓f[␈↓αa ␈↓↓u] ≥ bestsofar ␈↓αthen ␈↓↓bestmina[␈↓αd ␈↓↓u, sofar, bestsofar, f]
␈↓ ↓H␈↓↓ ␈↓αelse ␈↓↓bestmina[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u, f[␈↓αa ␈↓↓u], f].␈↓
␈↓ ↓H␈↓␈↓ α_However,␈α
this␈αstraight␈α
minimax␈α
computation␈αof␈α
the␈α
best␈αmove␈α
does␈α
much␈αmore␈α
computation
␈↓ ↓H␈↓in␈αgeneral␈αthan␈αis␈αusually␈αnecessary.␈α The␈αsimplest␈αway␈α
to␈αsee␈αthis␈αis␈αfrom␈αthe␈αmove␈αtree␈αof␈α
Figure
␈↓ ↓H␈↓2.6.
␈↓ ↓H␈↓ ≤ 8
␈↓ ↓H␈↓ max ≤'
␈↓ ↓H␈↓␈↓ α_≤≥
␈↓ ↓H␈↓ min ≤' `≥
␈↓ ↓H␈↓ ≤' ` 6
␈↓ ↓H␈↓␈↓ ¬H '␈↓ ↓H ≥
␈↓ ↓H␈↓ `≥ ≤ 9
␈↓ ↓H␈↓ `≥ ≤'
␈↓ ↓H␈↓␈↓ α_`'␈↓ ↓H αα X␈↓ ↓H ≥
␈↓ ↓H␈↓ `≥
␈↓ ↓H␈↓ ` X
␈↓ ↓H␈↓↔␈↓Figure 2.6␈↓
␈↓ ↓H␈↓We␈α∂see␈α∂from␈α∂this␈α∂figure␈α∂that␈α∂it␈α∂is␈α∂unnecessary␈α∂to␈α∂examine␈α∂the␈α∂moves␈α∂marked␈α∂x,␈α⊂because␈α∂their
␈↓ ↓H␈↓values␈αhave␈αno␈αeffect␈αon␈α
the␈αvalue␈αof␈αthe␈αgame␈α
or␈αon␈αthe␈αcorrect␈αmove␈α
since␈αthe␈αpresence␈αof␈αthe␈α
9
␈↓ ↓H␈↓is␈α∞sufficient␈α∞information␈α
at␈α∞this␈α∞point␈α
to␈α∞show␈α∞that␈α
the␈α∞move␈α∞leading␈α
to␈α∞the␈α∞vertex␈α∞preceding␈α
it
␈↓ ↓H␈↓should not be chosen by the minimizing player.
␈↓ ↓H␈↓␈↓ α_The␈α
general␈α
situation␈α
is␈α
that␈α
it␈α
is␈αunnecessary␈α
to␈α
examine␈α
further␈α
moves␈α
in␈α
a␈α
position␈αonce␈α
a
␈↓ ↓H␈↓move␈α
is␈α
found␈α
that␈α
refutes␈α∞moving␈α
to␈α
the␈α
position␈α
in␈α
the␈α∞first␈α
place.␈α
This␈α
idea␈α
is␈α
called␈α∞the␈α
β-
␈↓ ↓H␈↓heuristic and expressed in the following recursive definitions:
␈↓ ↓H␈↓␈↓ α_␈↓↓maxval p ← maxval1[p, -∞, ∞],
␈↓ ↓H␈↓ maxval1[p, , β] ← ␈↓αif ␈↓↓ter p ␈↓αthen ␈↓↓max[, min[β, imval p]] ␈↓αelse ␈↓↓maxval2[successors p, , β],
␈↓ ↓H␈↓␈↓ ¬jCHAPTER II␈↓ *36
␈↓ ↓H␈↓↓ maxval2[u, , β] ← {minval1[␈↓αa ␈↓↓u, , β]}[λw: ␈↓αif ␈↓↓w = β ␈↓αthen ␈↓↓β ␈↓αelse ␈↓↓maxval2[␈↓αd ␈↓↓u, w, β]],
␈↓ ↓H␈↓↓␈↓ α_minval p ← minval1[p, -∞, ∞],
␈↓ ↓H␈↓↓ minval1[p, , β] ← ␈↓αif ␈↓↓ter p ␈↓αthen ␈↓↓max[, min[β, imval p]] ␈↓αelse ␈↓↓minval2[successors p, , β],
␈↓ ↓H␈↓↓ minval2[u, , β] ← {maxval1[␈↓αa ␈↓↓u, , β]}[λw: ␈↓αif ␈↓↓w = ␈↓αthen ␈↓↓ ␈↓αelse ␈↓↓minval2[␈↓αd ␈↓↓u, , w].␈↓
␈↓ ↓H␈↓␈↓ α_The␈αreduction␈α
in␈αnumber␈αof␈α
positions␈αexamined␈αgiven␈α
by␈αthe␈αβ␈α
algorithm␈αover␈α
the␈αsimple
␈↓ ↓H␈↓minimax␈α
algorithm␈α
depends␈α
on␈α
the␈α
order␈α
in␈α
which␈α
the␈α
moves␈α
are␈α
examined.␈α
In␈α
the␈α∞worst␈α
case,
␈↓ ↓H␈↓the␈αmoves␈αhappen␈αto␈αbe␈αexamined␈αin␈αinverse␈αorder␈αof␈αmerit␈αin␈αevery␈αposition␈αon␈αthe␈αtree,␈αi.e.␈αthe
␈↓ ↓H␈↓worst␈αmove␈αfirst.␈α In␈αthat␈αcase,␈αthere␈αis␈αno␈αimprovement␈αover␈αminimax.␈α The␈αbest␈αcase␈αis␈αthe␈αone
␈↓ ↓H␈↓in␈α
which␈α
the␈α
best␈α
move␈α
in␈α
every␈α
position␈α
is␈α
examined␈α
first.␈α
If␈α
we␈α
look␈α
␈↓↓n␈↓␈α
moves␈α
deep␈α
on␈α
a␈αtree
␈↓ ↓H␈↓that␈αhas␈α␈↓↓k␈↓␈αsuccessors␈αto␈αeach␈αposition,␈αthen␈αminimax␈αlooks␈αat␈α␈↓↓k␈↓¬n␈↓␈αpositions␈αwhile␈αβ␈αlooks␈αat␈αabout
␈↓ ↓H␈↓only␈α∂␈↓↓k␈↓¬n/2␈↓.␈α∂ Thus␈α∂a␈α∞program␈α∂that␈α∂looks␈α∂at␈α∂10␈↓¬4␈↓␈α∞moves␈α∂with␈α∂β␈α∂might␈α∞have␈α∂to␈α∂look␈α∂at␈α∂10␈↓¬8␈↓␈α∞with
␈↓ ↓H␈↓minimax.␈α
For␈α
this␈α
reason,␈αgame␈α
playing␈α
programs␈α
using␈αβ␈α
make␈α
a␈α
big␈αeffort␈α
to␈α
include␈α
as␈αgood␈α
as
␈↓ ↓H␈↓possible␈α
an␈α
ordering␈α
of␈αmoves␈α
into␈α
the␈α
␈↓↓successors␈↓␈αfunction.␈α
When␈α
there␈α
is␈αa␈α
deep␈α
tree␈α
search␈αto␈α
be
␈↓ ↓H␈↓done,␈αthe␈αway␈αto␈αmake␈αthe␈αordering␈αis␈αwith␈αa␈αshort␈αlook-ahead␈αto␈αa␈αmuch␈αsmaller␈αdepth␈αthan␈αthe
␈↓ ↓H␈↓main␈α
search.␈α
Still␈α
shorter␈α
look-aheads␈α
are␈α
used␈α
deeper␈α
in␈α
the␈α
tree,␈α
and␈α
beyond␈α
a␈α
certain␈αdepth,
␈↓ ↓H␈↓non-look-ahead ordering methods are of decreasing complexity.
␈↓ ↓H␈↓␈↓ α_A␈α∂version␈α∞of␈α∂β␈α∞incorporating␈α∂optimistic␈α∞and␈α∂pessimistic␈α∞evaluations␈α∂of␈α∞positions␈α∂was␈α∞first
␈↓ ↓H␈↓proposed␈α∂by␈α∂McCarthy␈α∂about␈α∂1958.␈α∂ Edwards␈α∂and␈α∂Hart␈α∂at␈α∂M.I.T.␈α∂about␈α∂1959␈α∂proved␈α∂that␈α∞the
␈↓ ↓H␈↓present␈α∂version␈α⊂of␈α∂β␈α∂works␈α⊂and␈α∂calculated␈α∂the␈α⊂improvement␈α∂it␈α∂gives␈α⊂over␈α∂minimax.␈α⊂ The␈α∂first
␈↓ ↓H␈↓publication,␈α
however,␈α
was␈α
Brudno␈α
(1963).␈α
It␈α
is␈α
worth␈α
noting␈α
that␈α
β␈α
was␈α
not␈α
used␈α
in␈α
the␈α
early␈α
chess
␈↓ ↓H␈↓playing␈α∂programs␈α⊂in␈α∂spite␈α⊂of␈α∂the␈α⊂fact␈α∂that␈α⊂it␈α∂is␈α⊂clearly␈α∂used␈α⊂in␈α∂any␈α⊂human␈α∂play.␈α⊂ Its␈α∂non-use,
␈↓ ↓H␈↓therefore,␈α∩represents␈α∩a␈α⊃failure␈α∩of␈α∩self-observation.␈α⊃ Very␈α∩likely,␈α∩there␈α⊃are␈α∩a␈α∩number␈α∩of␈α⊃other
␈↓ ↓H␈↓algorithms␈α∞used␈α∞in␈α∞human␈α∞thought␈α∞that␈α∞we␈α∞have␈α∞not␈α∞noticed␈α∞an␈α∞incorporated␈α∞in␈α∞our␈α
programs.
␈↓ ↓H␈↓To␈αthe␈αextent␈αthat␈αthis␈αis␈αso,␈αartificial␈αintelligence␈αwill␈αbe␈α␈↓↓a␈αposteriori␈↓␈αobvious␈αeven␈αthough␈αit␈αis␈α␈↓↓a
␈↓ ↓H␈↓↓priori␈↓ very difficult.
␈↓ ↓H␈↓␈↓ εH␈↓ *37
␈↓ ↓H␈↓α␈↓ ¬zChapter III
␈↓ ↓H␈↓α␈↓ ¬/COMPILING IN LISP
␈↓ ↓H␈↓1. ␈↓αIntroduction␈↓
␈↓ ↓H␈↓␈↓ α_Compiling␈αis␈αan␈αimportant␈αexample␈αof␈αsymbolic␈αcomputation␈αand␈αhas␈αreceived␈αmuch␈αstudy.
␈↓ ↓H␈↓Much␈α
of␈α
the␈α
study␈α
has␈α
been␈α
devoted␈α
to␈α
parsing␈α
which␈α
is␈α
essentially␈α
the␈α
transformation␈α
of␈αan␈α
input
␈↓ ↓H␈↓string␈α⊂in␈α⊂the␈α∂source␈α⊂language␈α⊂into␈α∂an␈α⊂internal␈α⊂form.␈α∂ The␈α⊂internal␈α⊂form␈α∂used␈α⊂depends␈α⊂on␈α∂the
␈↓ ↓H␈↓compiler.␈α⊂ Sometimes␈α⊂it␈α⊂is␈α⊂Polish␈α⊂prefix␈α⊂or␈α∂postfix␈α⊂notation,␈α⊂sometimes␈α⊂it␈α⊂is␈α⊂list␈α⊂structure,␈α∂and
␈↓ ↓H␈↓sometimes it consists of entries in a collection of tables.
␈↓ ↓H␈↓␈↓ α_When␈αinternal␈αnotation␈αLISP␈αis␈αbeing␈αcompiled,␈αthe␈αparsing␈αis␈αtrivial,␈αbecause␈αthe␈αinput␈αis
␈↓ ↓H␈↓S-expressions␈α
and␈αthe␈α
internal␈α
form␈αwanted␈α
is␈α
list␈αstructure,␈α
so␈αwhat␈α
parsing␈α
there␈αis␈α
is␈α
done␈αby
␈↓ ↓H␈↓the␈α∞ordinary␈α∞LISP␈α∞␈↓↓read␈↓␈α∂routine.␈α∞ Therefore,␈α∞compilers␈α∞can␈α∂be␈α∞very␈α∞compact␈α∞and␈α∂transparent␈α∞in
␈↓ ↓H␈↓structure,␈αand␈α
we␈αcan␈α
concentrate␈αour␈α
attention␈αon␈αthe␈α
code␈αgeneration␈α
phase.␈α This␈α
is␈αas␈αit␈α
should
␈↓ ↓H␈↓be,␈α
because,␈αin␈α
my␈αopinion,␈α
parsing␈αis␈α
basically␈α
a␈αside␈α
issue␈αin␈α
compiling,␈αand␈α
code␈α
generation␈αis
␈↓ ↓H␈↓the matter of main scientific interest.
␈↓ ↓H␈↓␈↓ α_We␈α
shall␈αdescribe␈α
two␈α
compilers␈αin␈α
this␈αchapter␈α
called␈α
LCOM0␈αand␈α
LCOM4␈αwhich␈α
compile
␈↓ ↓H␈↓S-expression␈αLISP␈αinto␈αmachine␈α
language␈αfor␈αthe␈αPDP-10␈α
computer␈αaccording␈αto␈αthe␈α
conventions
␈↓ ↓H␈↓of␈α
Stanford␈α
LISP␈α
1.6.␈α
For␈α
now␈α
we␈α
shall␈α
take␈α
these␈α
conventions␈α
for␈α
granted.␈α
Before␈α
describing␈α
the
␈↓ ↓H␈↓compilers, we must describe these conventions.
␈↓ ↓H␈↓␈↓ α_The␈αtarget␈αlanguage␈αis␈αcalled␈αLAP␈αfor␈αLISP␈αassembly␈αprogram.␈α Each␈αfunction␈αis␈αcompiled
␈↓ ↓H␈↓separately␈αinto␈αa␈αseparate␈αLAP␈αprogram,␈αand␈αthese␈αprograms␈αare␈αwritten␈αonto␈αa␈αdisk␈αfile.␈α These
␈↓ ↓H␈↓files␈α∂can␈α∂later␈α∂be␈α∞read␈α∂into␈α∂a␈α∂LISP␈α∞core␈α∂image␈α∂and␈α∂assembled␈α∞in␈α∂place␈α∂by␈α∂the␈α∂LAP␈α∞assembler
␈↓ ↓H␈↓which␈αis␈αpart␈αof␈αthe␈αLISP␈αruntime␈αroutines.␈α
The␈αcompiler,␈αon␈αthe␈αother␈αhand,␈αis␈αa␈αseparate␈α
LISP
␈↓ ↓H␈↓core␈αimage␈αthat␈αcan␈αbe␈αinstructed␈αto␈αcompile␈αseveral␈αinput␈αfiles.␈α For␈αan␈αinput␈αfile␈αcalled␈α<name>,
␈↓ ↓H␈↓it␈α
produces␈αand␈α
output␈αfile␈α
called␈α
<name>.LAP.␈α All␈α
this␈αis␈α
specific␈αto␈α
the␈α
PDP-10␈αtime-sharing
␈↓ ↓H␈↓system␈αand␈αfortunately␈αworks␈αthe␈αsame␈αwhether␈αthe␈αtime-sharing␈αsystem␈αis␈αthe␈αD.E.C.␈αsystem,␈αthe
␈↓ ↓H␈↓Stanford system, or TENEX.
␈↓ ↓H␈↓␈↓ α_The␈αLAP␈α
program␈αproduced␈αis␈α
a␈αlist␈αof␈α
words;␈αthe␈α
last␈αword␈αis␈α
NIL,␈αand␈αthe␈α
first␈αword␈αis␈α
a
␈↓ ↓H␈↓header␈αof␈α
the␈αform␈α(LAP␈α
␈↓↓fname␈↓␈αSUBR)␈αwhere␈α
␈↓↓fname␈↓␈αis␈α
the␈αname␈αof␈α
the␈αfunction␈αcompiled.␈α
This
␈↓ ↓H␈↓header␈αtells␈αthe␈αDSKIN␈αprogram␈αthat␈αit␈αshould␈αread␈αS-expressions␈αtill␈αit␈αcomes␈αto␈αNIL␈αand␈αthen
␈↓ ↓H␈↓submit␈αwhat␈α
it␈αhas␈α
collected␈αto␈α
the␈αLAP␈α
assembler␈αwhich␈α
will␈αassemble␈α
it␈αinto␈α
core.␈α The␈α
rest␈αof
␈↓ ↓H␈↓the words are either atoms representing labels or lists representing single PDP-10 instructions.
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *38
␈↓ ↓H␈↓2. ␈↓αSome facts about the PDP-10.␈↓
␈↓ ↓H␈↓␈↓ α_The following facts about the PDP-10 may be of use:
␈↓ ↓H␈↓␈↓ α_The␈αPDP-10␈αhas␈αa␈α36␈αbit␈αword␈αand␈α
an␈α18␈αbit␈αaddress.␈α In␈αinstructions␈αand␈αin␈α
accumulators
␈↓ ↓H␈↓used␈α∩as␈α⊃index␈α∩registers␈α⊃the␈α∩address␈α∩is␈α⊃found␈α∩in␈α⊃the␈α∩right␈α∩part␈α⊃of␈α∩the␈α⊃word␈α∩where␈α∩the␈α⊃least
␈↓ ↓H␈↓significant bits in arithmetic reside.
␈↓ ↓H␈↓␈↓ α_There␈α
are␈α
16␈α
general␈α
registers␈α
which␈α
serve␈α
simultaneously␈α
as␈α
accumulators␈α
(receiving␈αthe
␈↓ ↓H␈↓results␈αof␈αarithmetic␈αoperations),␈αindex␈αregisters␈α(modifying␈αthe␈αnominal␈αaddresses␈αof␈α instructions
␈↓ ↓H␈↓to␈α
form␈α
effective␈α
addresses),␈α
and␈α
as␈α
the␈α
first␈α
16␈α
registers␈α
of␈α
memory␈α
(if␈α
the␈α
effective␈α
address␈α
of␈α
an
␈↓ ↓H␈↓instruction␈α
is␈α less␈α
than␈α 16,␈α
then␈α the␈α
instruction␈αuses␈α
the␈αcorresponding␈α
general␈αregister␈α
as␈αits
␈↓ ↓H␈↓operand.)
␈↓ ↓H␈↓␈↓ α_All␈α
instructions␈α
have␈α
the␈α
same␈α
format␈α
and␈α
are␈α
written␈α
for␈α
the␈α
LAP␈α
assembly␈α
program␈α
in␈α
the
␈↓ ↓H␈↓form
␈↓ ↓H␈↓␈↓ α_(<op name> <accumulator> <address> <index register>).
␈↓ ↓H␈↓Thus␈α
(MOVE␈α
1␈α
3␈α
P) ␈α
causes␈α
accumulator␈α 1 ␈α
to␈α
receive␈α
the␈α
contents␈α
of␈α
a␈α
memory␈α register␈α
whose
␈↓ ↓H␈↓address␈α
is␈α
3+c(P),␈α
i.e.␈α
3+<the␈α
contents␈α
of␈α
general␈α
register␈α
P>.␈α
If␈α
no␈α
index␈α
register␈α
is␈α
specified,␈α
then
␈↓ ↓H␈↓the␈α
address␈α
used␈αis␈α
just␈α
<address>.␈α
If␈αthe␈α
op␈α
name␈αis␈α
followed␈α
by␈α
@,␈αthen␈α
the␈α
memory␈αreference␈α
is
␈↓ ↓H␈↓indirect,␈αi.e.␈αthe␈α
effective␈αaddrress␈αis␈α
the␈αcontents␈αof␈α
the␈αright␈αhalf␈α
of␈αthe␈αword␈α directly␈α
addressed
␈↓ ↓H␈↓by␈α⊃ the␈α⊃ instruction␈α⊃(modified␈α⊃ by␈α⊃ the␈α⊃index␈α⊂register␈α⊃and␈α⊃indirect␈α⊃bit␈α⊃of␈α⊃that␈α⊃word).␈α⊃ In␈α⊂the
␈↓ ↓H␈↓following␈α∂description␈α∂of␈α∂some␈α∂instructions␈α∂useful␈α∞in␈α∂constructing␈α∂the␈α∂compiler,␈α∂<ef>␈α∂denotes␈α∞the
␈↓ ↓H␈↓effective address of an instruction.
␈↓ ↓H␈↓␈↓ αHMOVE ␈↓ ¬((ac) ← c(<ef>)
␈↓ ↓H␈↓␈↓ αHMOVEI ␈↓ ¬(c(ac) ← <ef>
␈↓ ↓H␈↓␈↓ αHMOVEM ␈↓ ¬(c(<ef>) ← c(ac)
␈↓ ↓H␈↓␈↓ αHHLRZ ␈↓ ¬(right half of c(ac) ← left half of c(<ef>)
␈↓ ↓H␈↓␈↓ αHHRRZ ␈↓ ¬(right half of c(ac) ← right half of c(<ef>)
␈↓ ↓H␈↓␈↓ αH␈↓ β8(These two are used indirectly for car and cdr respectively.)
␈↓ ↓H␈↓␈↓ αHADD ␈↓ ¬(c(ac) ← c(ac) + c(<ef>)
␈↓ ↓H␈↓␈↓ αHSUB ␈↓ ¬(c(ac) ← c(ac) - c(<ef>)
␈↓ ↓H␈↓␈↓ αHJRST ␈↓ ¬(go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPE ␈↓ ¬(if c(ac) = 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHJUMPN ␈↓ ¬(if c(ac) ≠ 0 then go to <ef>
␈↓ ↓H␈↓␈↓ αHCAME ␈↓ ¬(if c(ac) = c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHCAMN ␈↓ ¬(if c(ac) ≠ c(<ef>) then <skip next instruction>
␈↓ ↓H␈↓␈↓ αHPUSH ␈↓ ¬(c(c(right␈αhalf␈αof␈αac))␈α←␈αc(<ef>);␈αthe␈αcontents␈αof␈αeach
␈↓ ↓H␈↓␈↓ ¬(half␈α
of␈αac␈α
is␈α
increased␈αby␈α
one␈α
and␈αif␈α
the␈αcontents␈α
of
␈↓ ↓H␈↓␈↓ ¬(the␈α∞left␈α∞half␈α∞is␈α∞then␈α∞ 0,␈α∞a␈α∞stack␈α∞overflow␈α∞interrupt
␈↓ ↓H␈↓␈↓ ¬(occurs.␈α
(PUSH␈α
P␈α
ac)␈α
is␈α
is␈α
used␈α
in␈α
LISP␈α
to␈α
put␈α
c(ac)
␈↓ ↓H␈↓␈↓ ¬(on the stack.
␈↓ ↓H␈↓␈↓ αHPOP ␈↓ ¬(c(<ef>)␈α
←c(c(right␈α∞half␈α
of␈α
ac));␈α∞the␈α
contents␈α∞of␈α
each
␈↓ ↓H␈↓␈↓ ¬(half␈α∞of␈α∞ac␈α∞are␈α∞then␈α∞decreased␈α∞by␈α∞1.␈α∞ This␈α∂may␈α∞be
␈↓ ↓H␈↓␈↓ ¬(used␈α∩for␈α⊃removing␈α∩the␈α∩top␈α⊃element␈α∩of␈α∩the␈α⊃stack.
␈↓ ↓H␈↓␈↓ ¬(Thus␈α(POP␈αP␈α
1)␈αputs␈αthe␈α
top␈αelement␈αof␈α
the␈αstack
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *39
␈↓ ↓H␈↓␈↓ ¬(in␈αaccumulator␈α1.␈α The␈αi-th␈αelement␈αof␈αthe␈α
stack␈αis
␈↓ ↓H␈↓␈↓ ¬(obtained by (MOVE@ 1 i P).
␈↓ ↓H␈↓␈↓ αHPOPJ ␈↓ ¬((POPJ P) is used for returning from a subroutine
␈↓ ↓H␈↓␈↓ α_These␈α∞instructions␈α∞are␈α∞adequate␈α∞for␈α∞compiling␈α∂basic␈α∞LISP␈α∞code␈α∞with␈α∞ the␈α∞addition␈α∂of␈α∞the
␈↓ ↓H␈↓subroutine␈α∀calling␈α∀pseudo-instrucion.␈α∃(CALL␈α∀␈↓↓n␈α∀(E␈α∃<subr)␈α∀S)␈α∀is␈α∃used␈α∀for␈α∀calling␈α∃the␈α∀LISP
␈↓ ↓H␈↓↓subroutine␈α
<subr>␈α
with␈α∞ n␈↓␈α
arguments.␈α
The␈α∞ convention␈α
is␈α
that␈α
the␈α∞arguments␈α
will␈α
be␈α∞stored␈α
in
␈↓ ↓H␈↓successive␈α∩accumulators␈α∩beginning␈α∩with␈α⊃accumulator␈α∩ 1,␈α∩and␈α∩the␈α⊃result␈α∩will␈α∩ be␈α∩ returned␈α⊃ in
␈↓ ↓H␈↓accumulator␈α 1.␈α In␈αparticular␈αthe␈αfunctions␈α ATOM ␈α
and␈α CONS ␈αare␈αcalled␈αwith␈α (CALL␈α1␈α
(E
␈↓ ↓H␈↓ATOM)␈αS) ␈αand␈α
(CALL␈α2␈α (E␈αCONS)␈α
S)␈α respectively,␈α and␈α
programs␈αproduced␈αby␈αyou␈α
compiler
␈↓ ↓H␈↓will be called similarly when their names are referred to.
␈↓ ↓H␈↓3. ␈↓αCode produced by LISP compilers.␈↓
␈↓ ↓H␈↓␈↓ α_We␈αwill␈αdiscuss␈α
two␈αcompilers,␈αa␈αsimple␈α
one␈αcalled␈αLCOM0␈α
and␈αa␈αmore␈αoptimising␈α
compiler
␈↓ ↓H␈↓called␈αLCOM4.␈α LCOM4␈αproduces␈α
about␈αhalf␈αas␈αmany␈αinstructions␈α
for␈αa␈αgiven␈αfunction␈α
as␈αdoes
␈↓ ↓H␈↓LCOM0.␈α∩Besides␈α∩these,␈α∩there␈α∪is␈α∩the␈α∩standard␈α∩PDP-10␈α∪LISP␈α∩compiler␈α∩written␈α∩at␈α∪M.I.T.␈α∩ by
␈↓ ↓H␈↓Richard Greenblatt and Stuart Nelson and modified by Whitfield Diffie.
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *40
␈↓ ↓H␈↓Examples of output of LCOM0, LCOM4, and the regular Lisp compiler
␈↓ ↓H␈↓For the file containing:
␈↓ ↓H␈↓(DEFPROP DROP
␈↓ ↓H␈↓ (LAMBDA(X)
␈↓ ↓H␈↓ (COND ((NULL X) NIL) (T (CONS (LIST (CAR X)) (DROP (CDR X))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓LCOM0 produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E NULL) S)
␈↓ ↓H␈↓(JUMPE 1 G0163)
␈↓ ↓H␈↓(MOVEI 1 0)
␈↓ ↓H␈↓(JRST G0162)
␈↓ ↓H␈↓G0163
␈↓ ↓H␈↓(MOVEI 1 (QUOTE T))
␈↓ ↓H␈↓(JUMPE 1 G0164)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E CAR) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E LIST) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 -1 P)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E CDR) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 1 (E DROP) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 -1 P)
␈↓ ↓H␈↓(MOVE 2 0 P)
␈↓ ↓H␈↓(SUB P (C 2 0 2 0))
␈↓ ↓H␈↓(CALL 2 (E CONS) S)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *41
␈↓ ↓H␈↓(JRST G0162)
␈↓ ↓H␈↓G0164
␈↓ ↓H␈↓G0162
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(POPJ P)
␈↓ ↓H␈↓NIL
␈↓ ↓H␈↓LCOM4 produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(JUMPE 1 G0162)
␈↓ ↓H␈↓(HLRZ@ 1 0 P)
␈↓ ↓H␈↓(CALL 1 (E LIST) S)
␈↓ ↓H␈↓(PUSH P 1)
␈↓ ↓H␈↓(HRRZ@ 1 -1 P)
␈↓ ↓H␈↓(CALL 1 (E DROP) S)
␈↓ ↓H␈↓(MOVE 2 1)
␈↓ ↓H␈↓(MOVE 1 0 P)
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(CALL 2 (E CONS) S)
␈↓ ↓H␈↓G0162
␈↓ ↓H␈↓(SUB P (C 1 0 1 0))
␈↓ ↓H␈↓(POPJ P)
␈↓ ↓H␈↓NIL
␈↓ ↓H␈↓******************************************************************
␈↓ ↓H␈↓The ILISP compiler produces the following code:
␈↓ ↓H␈↓(LAP DROP SUBR)
␈↓ ↓H␈↓ (PUSH P 1)
␈↓ ↓H␈↓ (JUMPE 1 TAG1)
␈↓ ↓H␈↓ (HLRZ@ 1 0 P)
␈↓ ↓H␈↓ (CALL 1 (E NCONS) S)
␈↓ ↓H␈↓ (PUSH P 1)
␈↓ ↓H␈↓ (HRRZ@ 1 -1 P)
␈↓ ↓H␈↓ (CALL 1 (E DROP) S)
␈↓ ↓H␈↓ (POP P 2)
␈↓ ↓H␈↓ (CALL 2 (E XCONS) S)
␈↓ ↓H␈↓ TAG1 (SUB P (C 1 0 1 0))
␈↓ ↓H␈↓ (POPJ P)
␈↓ ↓H␈↓ NIL
␈↓ ↓H␈↓␈↓ α_Note␈α that␈αall␈αthree␈αcompilers␈αproduce␈αthe␈αsame␈αfirst␈αline␈αof␈αheading.␈α This␈αis␈αnecessary␈αfor
␈↓ ↓H␈↓the␈α∂LAP␈α⊂assembly␈α∂program␈α⊂ which␈α∂ also␈α⊂requires␈α∂ the␈α∂ NIL ␈α⊂at␈α∂the␈α⊂end␈α∂as␈α⊂punctuation.␈α∂ The
␈↓ ↓H␈↓standard␈αcompiler␈αuses␈αa␈αfunction␈αcalled␈α XCONS ␈αthat␈αhas␈α the␈α same␈α effect␈α as␈α CONS ␈α
except
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *42
␈↓ ↓H␈↓that␈α it␈αreceives␈αits␈αarguments␈αin␈αthe␈αreverse␈αorder␈αwhich␈αis␈αsometimes␈αconvenient.␈α This␈αrequires,
␈↓ ↓H␈↓of␈α
course,␈αthat␈α
the␈αcompiler␈α
be␈αsmart␈α
enough␈α
to␈α decide␈α
where␈α it␈α
is␈α more␈α
convenient␈α
to␈αput
␈↓ ↓H␈↓the arguments of the ␈↓↓cons␈↓ function.
␈↓ ↓H␈↓␈↓ α_My␈α two␈α compilers␈α
are␈α similar␈αin␈α
structure,␈αbut␈αthe␈α
better␈αone,␈αwhich␈α
is␈αtwice␈αas␈αlong,␈α
uses
␈↓ ↓H␈↓the following optimisations.
␈↓ ↓H␈↓␈↓ α_1.␈α
When␈α∞the␈α
argument␈α
of␈α∞CAR␈α
or␈α∞CDR␈α
is␈α
a␈α∞variable,␈α
it␈α∞compiles␈α
a␈α
(HLRZ@␈α∞ 1␈α
i␈α∞P)␈α
or
␈↓ ↓H␈↓(HRRZ@␈α1␈αi␈αP)␈αwhich␈αgets␈αthe␈αresult␈αthrough␈αthe␈αstack␈αwithout␈αfirst␈αcompiling␈αthe␈αargument␈αinto
␈↓ ↓H␈↓an accumulator.
␈↓ ↓H␈↓␈↓ α_2.␈αWhen␈αit␈αhas␈αto␈αset␈αup␈αthe␈αarguments␈αof␈αa␈αfunction␈α in␈α the␈αaccumulators,␈α in␈αgeneral,␈αthe
␈↓ ↓H␈↓program␈α
must␈α
compute␈α
the␈α
arguments␈α
one␈α
at␈α
a␈αtime␈α
and␈α
save␈α
them␈α
on␈α
the␈α
stack,␈α
and␈α
then␈αload
␈↓ ↓H␈↓the␈α∞ accumulators␈α∞from␈α∞the␈α∞stack.␈α∞ However,␈α∞if␈α∂one␈α∞of␈α∞the␈α∞arguments␈α∞is␈α∞a␈α∞variable,␈α∞is␈α∂a␈α∞quoted
␈↓ ↓H␈↓expression,␈αor␈αcan␈αbe␈α
obtained␈αfrom␈αa␈αvariable␈α
by␈αa␈α chain␈α of␈α
cars ␈α and␈α cdrs,␈α then␈α
it␈α need
␈↓ ↓H␈↓not␈αbe␈αcomputed␈α
until␈αthe␈αtime␈α
of␈αloading␈α accumulators␈α
since␈α it␈α can␈α
be␈α computed␈α using␈α
only
␈↓ ↓H␈↓the accumulator in which it is wanted.
␈↓ ↓H␈↓␈↓ α_3.␈α⊃ The␈α⊂ Diffy␈α⊃ compiler␈α⊂ produces␈α⊃about␈α⊂10␈α⊃percent␈α⊂less␈α⊃code␈α⊂than␈α⊃LCOM4;␈α⊃the␈α⊂main
␈↓ ↓H␈↓difference␈α∞seems␈α∞to␈α∂be␈α∞that␈α∞it␈α∂sometimes␈α∞notices␈α∞when␈α∂ it␈α∞ has␈α∞ an␈α∂ expression␈α∞that␈α∞it␈α∂will␈α∞need
␈↓ ↓H␈↓later.␈αSometimes␈αthis␈αfeature␈αleads␈αit␈α
astray.␈αFor␈αexample,␈αit␈αmay␈αsave␈α
␈↓αa␈α␈↓↓u␈↓ ␈αfor␈αlater␈αuse␈αat␈α
greater
␈↓ ↓H␈↓cost than re-computing it.
␈↓ ↓H␈↓␈↓ α_It should further be noted that quoted S-expressions can be compiled with the instruction
␈↓ ↓H␈↓␈↓ α_(MOVEI 1 (QUOTE )).
␈↓ ↓H␈↓4. ␈↓αListings of LCOM0 and LCOM4␈↓
␈↓ ↓H␈↓␈↓ α_First␈α∩are␈α∪listings␈α∩of␈α∪both␈α∩compilers␈α∪in␈α∩blackboard␈α∪notation.␈α∩ Following␈α∪these␈α∩is␈α∪an␈α∩an
␈↓ ↓H␈↓annotated listing of LCOM0 in S-expression notation, and a listing of LCOM4 in that notation.
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *43
␈↓ ↓H␈↓LCOM0:
␈↓ ↓H␈↓␈↓ ↓x␈↓↓compl file (FEXPR) ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αprog [␈↓↓z]
␈↓ ↓H␈↓↓␈↓ αaeval OUTPUT . [ `DSK:' . <␈↓αa ␈↓↓file . LAP>]
␈↓ ↓H␈↓↓␈↓ αaeval INPUT . [ `DSK:' . file]
␈↓ ↓H␈↓↓␈↓ αainc[T, NIL]
␈↓ ↓H␈↓↓␈↓ αaoutc[T, NIL]
␈↓ ↓H␈↓↓␈↓ α#loop z ← errset read[]
␈↓ ↓H␈↓↓␈↓ αa␈↓αif at ␈↓↓z ␈↓αthen ␈↓↓go done ␈↓αelse if T then NIL
␈↓ ↓H␈↓α␈↓ αa␈↓↓z ← ␈↓αa ␈↓↓z
␈↓ ↓H␈↓↓␈↓ αa␈↓αif a ␈↓↓z ␈↓αeq DE then
␈↓ ↓H␈↓α␈↓ β↓[prog [␈↓↓prog]
␈↓ ↓H␈↓↓␈↓ βSprog ← comp[␈↓αad ␈↓↓z, ␈↓αadd ␈↓↓z, ␈↓αaddd ␈↓↓z]
␈↓ ↓H␈↓↓␈↓ βSmapc[print, prog]
␈↓ ↓H␈↓↓␈↓ βSoutc[NIL, NIL]
␈↓ ↓H␈↓↓␈↓ βSprint <␈↓αad ␈↓↓z, length prog>
␈↓ ↓H␈↓↓␈↓ βSoutc[T, NIL]]
␈↓ ↓H␈↓↓␈↓ αq␈↓αelse ␈↓↓print z
␈↓ ↓H␈↓↓␈↓ αago loop
␈↓ ↓H␈↓↓␈↓ α≥done outc[NIL, T]
␈↓ ↓H␈↓↓␈↓ αainc[NIL, T]
␈↓ ↓H␈↓↓␈↓ αareturn ENDCOMP
␈↓ ↓H␈↓↓␈↓ ↓xcomp[fn, vars, exp] ←
␈↓ ↓H␈↓↓␈↓ α_{length vars}[λn.
␈↓ ↓H␈↓↓␈↓ α8<<LAP, fn, SUBR>>
␈↓ ↓H␈↓↓␈↓ α8* mkpush[n, 1]
␈↓ ↓H␈↓↓␈↓ α8* compexp[exp, -n, prup[vars, 1]]
␈↓ ↓H␈↓↓␈↓ α8* <<SUB, P, <C, n, 0, n, 0>>>
␈↓ ↓H␈↓↓␈↓ α8* ((POPJ P) NIL)]
␈↓ ↓H␈↓↓␈↓ ↓xprup[vars, n] ← ␈↓αif n ␈↓↓vars ␈↓αthen NIL else [a ␈↓↓vars . n] . prup[␈↓αd ␈↓↓vars, n + 1]
␈↓ ↓H␈↓↓␈↓ ↓xmkpush[n, m] ← ␈↓αif ␈↓↓n < m ␈↓αthen NIL else <PUSH, P, ␈↓↓m> . mkpush[n, m + 1]
␈↓ ↓H␈↓↓␈↓ ↓xcompexp[exp, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓exp ␈↓αthen ((MOVEI 1 0))
␈↓ ↓H␈↓α␈↓ α_else if ␈↓↓exp ␈↓αeq T then ((MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓α␈↓ α_else if at ␈↓↓exp ␈↓αthen <<MOVE, 1, ␈↓↓m + ␈↓αd ␈↓↓assoc[exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq AND ∨ a ␈↓↓exp ␈↓αeq OR ∨ a ␈↓↓exp ␈↓αeq NOT then
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *44
␈↓ ↓H␈↓α␈↓ α8{␈↓↓gensym[], gensym[]}[λl1, l2.
␈↓ ↓H␈↓↓␈↓ αXcombool[exp, m, l1, NIL, vpr]
␈↓ ↓H␈↓↓␈↓ αX* <(MOVEI 1 (QUOTE T)), <JRST, 0, l2>, l1, (MOVEI 1 0), l2>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq COND then ␈↓↓comcond[␈↓αd ␈↓↓exp, m, gensym[], vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq QUOTE then <<MOVEI, 1, ␈↓↓exp>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a ␈↓↓exp ␈↓αthen
␈↓ ↓H␈↓α␈↓ α8{␈↓↓length ␈↓αd ␈↓↓exp}[λn.
␈↓ ↓H␈↓↓␈↓ αXcomplis[␈↓αd ␈↓↓exp, m, vpr]
␈↓ ↓H␈↓↓␈↓ αX* loadac[1 - n, 1]
␈↓ ↓H␈↓↓␈↓ αX* <<SUB, P, <C, n, 0, n, 0>>>
␈↓ ↓H␈↓↓␈↓ αX* <<CALL, n, <E, ␈↓αa ␈↓↓exp>, S>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓exp ␈↓αeq LAMBDA then
␈↓ ↓H␈↓α␈↓ α8{␈↓↓length ␈↓αd ␈↓↓exp}[λn.
␈↓ ↓H␈↓↓␈↓ αXcomplis[␈↓αd ␈↓↓exp, m, vpr]
␈↓ ↓H␈↓↓␈↓ αX* compexp[␈↓αadda ␈↓↓exp, m - n, prup[␈↓αada ␈↓↓exp, 1 - m] * vpr]
␈↓ ↓H␈↓↓␈↓ αX* <<SUB, P, <C, n, 0, n, 0>>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if T then NIL
␈↓ ↓H␈↓α␈↓ ↓x␈↓↓complis[u, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen NIL else ␈↓↓compexp[␈↓αa ␈↓↓u, m, vpr] * ((PUSH P 1)) * complis[␈↓αd ␈↓↓u, m - 1, vpr]
␈↓ ↓H␈↓↓␈↓ ↓xloadac[n, k] ← ␈↓αif ␈↓↓n > 0 ␈↓αthen NIL else <MOVE, ␈↓↓k, n, P> . loadac[n + 1, k + 1]
␈↓ ↓H␈↓↓␈↓ ↓xcomcond[u, m, l, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen <␈↓↓l>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse {␈↓↓gensym[]}[λl1.
␈↓ ↓H␈↓↓␈↓ α8combool[␈↓αaa ␈↓↓u, m, l1, NIL, vpr]
␈↓ ↓H␈↓↓␈↓ α8* compexp[␈↓αada ␈↓↓u, m, vpr]
␈↓ ↓H␈↓↓␈↓ α8* <<JRST, l>, l1>
␈↓ ↓H␈↓↓␈↓ α8* comcond[␈↓αd ␈↓↓u, m, l, vpr]]
␈↓ ↓H␈↓↓␈↓ ↓xcombool[p, m, l, flg, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif at ␈↓↓p ␈↓αthen [␈↓↓compexp[p, m, vpr] * <<␈↓αif ␈↓↓flg ␈↓αthen JUMPN else JUMPE, 1, ␈↓↓l>>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq AND then
␈↓ ↓H␈↓α␈↓ α8[if ¬␈↓↓flg ␈↓αthen ␈↓↓compandor[␈↓αd ␈↓↓p, m, l, NIL, vpr]
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse {␈↓↓gensym[]}[λl1. compandor[␈↓αd ␈↓↓p, m, l1, NIL, vpr] * <<JRST, 0, l>> * <l1>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq OR then
␈↓ ↓H␈↓α␈↓ α8[if ␈↓↓flg ␈↓αthen ␈↓↓compandor[␈↓αd ␈↓↓p, m, l, T, vpr]
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse {␈↓↓gensym[]}[λl1. compandor[␈↓αd ␈↓↓p, m, l1, T, vpr] * <<JRST, 0, l>> * <l1>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq NOT then ␈↓↓combool[␈↓αad ␈↓↓p, m, l, ¬flg, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse ␈↓↓compexp[p, m, vpr] * <<␈↓αif ␈↓↓flg ␈↓αthen JUMPN else JUMPE, 1, ␈↓↓l>>
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *45
␈↓ ↓H␈↓↓␈↓ ↓xcompandor[u, m, l, flg, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen NIL else ␈↓↓combool[␈↓αa ␈↓↓u, m, l, flg, vpr] * compandor[␈↓αd ␈↓↓u, m, l, flg, vpr]
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *46
␈↓ ↓H␈↓LCOM4:
␈↓ ↓H␈↓␈↓ ↓x␈↓↓compl file (FEXPR) ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αprog [␈↓↓z]
␈↓ ↓H␈↓↓␈↓ αaeval OUTPUT . [ `DSK:' . <␈↓αa ␈↓↓file . LAP>]
␈↓ ↓H␈↓↓␈↓ αaeval INPUT . [ `DSK:' . file]
␈↓ ↓H␈↓↓␈↓ αainc[T, NIL]
␈↓ ↓H␈↓↓␈↓ αaoutc[T, NIL]
␈↓ ↓H␈↓↓␈↓ α#loop z ← errset read[]
␈↓ ↓H␈↓↓␈↓ αa␈↓αif at ␈↓↓z ␈↓αthen ␈↓↓go done ␈↓αelse if T then NIL
␈↓ ↓H␈↓α␈↓ αa␈↓↓z ← ␈↓αa ␈↓↓z
␈↓ ↓H␈↓↓␈↓ αa␈↓αif a ␈↓↓z ␈↓αeq DE ∨ [a ␈↓↓z ␈↓αeq DEFPROP ∧ addd ␈↓↓z ␈↓αeq EXPR] then
␈↓ ↓H␈↓α␈↓ β↓[prog [␈↓↓prog]
␈↓ ↓H␈↓↓␈↓ βSprog
␈↓ ↓H␈↓↓␈↓ βc← [␈↓αif a ␈↓↓z ␈↓αeq DE then ␈↓↓comp[␈↓αad ␈↓↓z, ␈↓αadd ␈↓↓z, ␈↓αaddd ␈↓↓z]
␈↓ ↓H␈↓↓␈↓ ∧ ␈↓αelse ␈↓↓comp[␈↓αad ␈↓↓z, ␈↓αad add ␈↓↓z, ␈↓αadd add ␈↓↓z]]
␈↓ ↓H␈↓↓␈↓ βSmapc[print, prog]
␈↓ ↓H␈↓↓␈↓ βSoutc[NIL, NIL]
␈↓ ↓H␈↓↓␈↓ βSprint <␈↓αad ␈↓↓z, length prog>
␈↓ ↓H␈↓↓␈↓ βSoutc[T, NIL]]
␈↓ ↓H␈↓↓␈↓ αq␈↓αelse ␈↓↓print z
␈↓ ↓H␈↓↓␈↓ αago loop
␈↓ ↓H␈↓↓␈↓ α≥done outc[NIL, T]
␈↓ ↓H␈↓↓␈↓ αainc[NIL, T]
␈↓ ↓H␈↓↓␈↓ αareturn ENDCOMP
␈↓ ↓H␈↓↓␈↓ ↓xcomp[fn, vars, exp] ←
␈↓ ↓H␈↓↓␈↓ α_{prup[vars, 1], length vars}[λvpr, n.
␈↓ ↓H␈↓↓␈↓ α8flat[<<<LAP, fn, SUBR>>,
␈↓ ↓H␈↓↓␈↓ β↓mkpush[n, 1],
␈↓ ↓H␈↓↓␈↓ β↓compexp[exp, -n, vpr],
␈↓ ↓H␈↓↓␈↓ β↓substack n,
␈↓ ↓H␈↓↓␈↓ β↓((POPJ P) (LABEL NIL))>,
␈↓ ↓H␈↓↓␈↓ αoNIL]]
␈↓ ↓H␈↓↓␈↓ ↓xsubstack n ← ␈↓αif ␈↓↓n ␈↓αeq 0 then NIL else <<SUB, P, <C, ␈↓↓n, 0, n, 0>>>
␈↓ ↓H␈↓↓␈↓ ↓xprup[vars, n] ← ␈↓αif n ␈↓↓vars ␈↓αthen NIL else [a ␈↓↓vars . n] . prup[␈↓αd ␈↓↓vars, n + 1]
␈↓ ↓H␈↓↓␈↓ ↓xmkpush[n, m] ← ␈↓αif ␈↓↓n < m ␈↓αthen NIL else <PUSH, P, ␈↓↓m> . mkpush[n, m + 1]
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *47
␈↓ ↓H␈↓↓␈↓ ↓xcompexp[exp, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓exp ␈↓αthen ((MOVEI 1 0))
␈↓ ↓H␈↓α␈↓ α_else if ␈↓↓exp ␈↓αeq T ∨ ␈↓↓numberp exp ␈↓αthen <<MOVEI, 1, <QUOTE, ␈↓↓exp>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at ␈↓↓exp ␈↓αthen <<MOVE, 1, ␈↓↓m + ␈↓αd ␈↓↓assoc[exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq CAR then
␈↓ ↓H␈↓α␈↓ α8[if at ad ␈↓↓exp ␈↓αthen << `HLRZ@' , 1, ␈↓↓m + ␈↓αd ␈↓↓assoc[␈↓αad ␈↓↓exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse <␈↓↓compexp[␈↓αad ␈↓↓exp, m, vpr], (( `HLRZ@' 1 1))>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq CDR then
␈↓ ↓H␈↓α␈↓ α8[if at ad ␈↓↓exp ␈↓αthen << `HRRZ@' , 1, ␈↓↓m + ␈↓αd ␈↓↓assoc[␈↓αad ␈↓↓exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse <␈↓↓compexp[␈↓αad ␈↓↓exp, m, vpr], (( `HRRZ@' 1 1))>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq AND ∨ a ␈↓↓exp ␈↓αeq OR ∨ a ␈↓↓exp ␈↓αeq NOT ∨ a ␈↓↓exp ␈↓αeq EQ then
␈↓ ↓H␈↓α␈↓ α8{␈↓↓gensym[], gensym[]}[λl1, l2.
␈↓ ↓H␈↓↓␈↓ αX<combool[exp, m, l1, NIL, vpr],
␈↓ ↓H␈↓↓␈↓ αj<(MOVEI 1 (QUOTE T)), <JRST, 0, l2>, <LABEL, l1>, (MOVEI 1 0), <LABEL
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq COND then ␈↓↓comcond[␈↓αd ␈↓↓exp, m, gensym[], vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq QUOTE then <<MOVEI, 1, ␈↓↓exp>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a ␈↓↓exp ␈↓αthen <␈↓↓complisa[␈↓αd ␈↓↓exp, m, vpr], <<CALL, length ␈↓αd ␈↓↓exp, <E, ␈↓αa ␈↓↓exp>, S>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓exp ␈↓αeq LAMBDA then
␈↓ ↓H␈↓α␈↓ α8{␈↓↓length ␈↓αd ␈↓↓exp}[λn.
␈↓ ↓H␈↓↓␈↓ αX<stackup[␈↓αd ␈↓↓exp, m, vpr],
␈↓ ↓H␈↓↓␈↓ αjcompexp[␈↓αadda ␈↓↓exp, m - n, apend[prup[␈↓αada ␈↓↓exp, 1 - m], vpr]],
␈↓ ↓H␈↓↓␈↓ αjsubstack n>]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if T then NIL
␈↓ ↓H␈↓α␈↓ ↓x␈↓↓stackup[u, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen NIL else <␈↓↓compexp[␈↓αa ␈↓↓u, m, vpr], ((PUSH P 1)), stackup[␈↓αd ␈↓↓u, m - 1, vpr]>
␈↓ ↓H␈↓↓␈↓ ↓xccchain exp ← [␈↓αa ␈↓↓exp ␈↓αeq CAR ∨ a ␈↓↓exp ␈↓αeq CDR] ∧ [at ad ␈↓↓exp ∨ ccchain ␈↓αad ␈↓↓exp]
␈↓ ↓H␈↓↓␈↓ ↓xcompc[exp, n2, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif at ␈↓↓exp ␈↓αthen ␈↓↓err COMPC
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓exp ␈↓αeq CAR then
␈↓ ↓H␈↓α␈↓ α8[if at ad ␈↓↓exp ␈↓αthen << `HLRZ@' , ␈↓↓n2, m + ␈↓αd ␈↓↓assoc[␈↓αad ␈↓↓exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse < `HLRZ@' , ␈↓↓n2, n2> . compc[␈↓αad ␈↓↓exp, n2, m, vpr]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at ad ␈↓↓exp ␈↓αthen << `HRRZ@' , ␈↓↓n2, m + ␈↓αd ␈↓↓assoc[␈↓αad ␈↓↓exp, vpr], P>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse < `HRRZ@' , ␈↓↓n2, n2> . compc[␈↓αad ␈↓↓exp, n2, m, vpr]
␈↓ ↓H␈↓↓␈↓ ↓xcomcond[u, m, l, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen <<LABEL, ␈↓↓l>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if ¬at aa ␈↓↓u ∧ ␈↓αaaa ␈↓↓u ␈↓αeq NULL ∧ n ada ␈↓↓u ␈↓αthen
␈↓ ↓H␈↓α␈↓ α8<␈↓↓compexp[␈↓αadaa ␈↓↓u, m, vpr], <<JUMPE, 1, l>>, comcond[␈↓αd ␈↓↓u, m, l, vpr]>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓u ␈↓αeq T then <␈↓↓compexp[␈↓αada ␈↓↓u, m, vpr], <<LABEL, l>>>
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *48
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse {␈↓↓gensym[]}[λl1.
␈↓ ↓H␈↓↓␈↓ α8<combool[␈↓αaa ␈↓↓u, m, l1, NIL, vpr],
␈↓ ↓H␈↓↓␈↓ αJcompexp[␈↓αada ␈↓↓u, m, vpr],
␈↓ ↓H␈↓↓␈↓ αJ<<JRST, 0, l>, <LABEL, l1>>,
␈↓ ↓H␈↓↓␈↓ αJcomcond[␈↓αd ␈↓↓u, m, l, vpr]>]
␈↓ ↓H␈↓↓␈↓ ↓xcomplisa[u, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_{classify u}[λz.
␈↓ ↓H␈↓↓␈↓ α8<complis[z, m, 1, vpr], loadac[z, 1 - ccount z, 1, m - ccount z, vpr], substack ccount z
␈↓ ↓H␈↓↓␈↓ ↓xccount z ← ␈↓αif n ␈↓↓z ␈↓αthen 0 else if aa ␈↓↓z ␈↓αeq 4 then 1 + ␈↓↓ccount ␈↓αd ␈↓↓z ␈↓αelse ␈↓↓ccount ␈↓αd ␈↓↓z
␈↓ ↓H␈↓↓␈↓ ↓xloadac[z, m2, n2, m, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓z ␈↓αthen NIL
␈↓ ↓H␈↓α␈↓ α_else if aa ␈↓↓z ␈↓αeq 1 then
␈↓ ↓H␈↓α␈↓ α8<MOVE, ␈↓↓n2, m + ␈↓αd ␈↓↓assoc[␈↓αda ␈↓↓z, vpr], P> . loadac[␈↓αd ␈↓↓z, m2, n2 + 1, m, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓z ␈↓αeq 0 then <MOVEI, ␈↓↓n2, <QUOTE, ␈↓αda ␈↓↓z>> . loadac[␈↓αd ␈↓↓z, m2, n2 + 1, m, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓z ␈↓αeq 2 then <MOVEI, ␈↓↓n2, ␈↓αda ␈↓↓z> . loadac[␈↓αd ␈↓↓z, m2, n2 + 1, m, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓z ␈↓αeq 3 then <␈↓↓reverse compc[␈↓αda ␈↓↓z, n2, m, vpr], loadac[␈↓αd ␈↓↓z, m2, n2 + 1, m, vpr]>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓z ␈↓αeq 5 then ␈↓↓loadac[␈↓αd ␈↓↓z, 1, n2 + 1, m, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse <MOVE, ␈↓↓n2, m2, P> . loadac[␈↓αd ␈↓↓z, m2 + 1, n2 + 1, m, vpr]
␈↓ ↓H␈↓↓␈↓ ↓xcomplis[z, m, k, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓z ␈↓αthen NIL
␈↓ ↓H␈↓α␈↓ α_else if aa ␈↓↓z ␈↓αeq 4 then <␈↓↓compexp[␈↓αda ␈↓↓z, m, vpr], ((PUSH P 1)), complis[␈↓αd ␈↓↓z, m - 1, k + 1,
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓z ␈↓αeq 5 then <␈↓↓compexp[␈↓αda ␈↓↓z, m, vpr], ␈↓αif ␈↓↓k ␈↓αeq 1 then NIL else <<MOVE, ␈↓↓k, 1>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse ␈↓↓complis[␈↓αd ␈↓↓z, m, k + 1, vpr]
␈↓ ↓H␈↓↓␈↓ ↓xclassify u ← class2[class1[u, NIL], NIL, T]
␈↓ ↓H␈↓↓␈↓ ↓xclass1[u, v] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓v
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a ␈↓↓u ␈↓αthen
␈↓ ↓H␈↓α␈↓ α8[if ␈↓↓equal[␈↓αa ␈↓↓u, NIL] ∨ equal[␈↓αa ␈↓↓u, T] ∨ numberp
␈↓ ↓H␈↓↓␈↓ α_␈↓αa ␈↓↓u ␈↓αthen ␈↓↓class1[␈↓αd ␈↓↓u, [0 . ␈↓αa ␈↓↓u] . v]
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse ␈↓↓class1[␈↓αd ␈↓↓u, [1 . ␈↓αa ␈↓↓u] .
␈↓ ↓H␈↓↓v]]␈↓ α_␈↓αelse if ␈↓↓equal[␈↓αaa ␈↓↓u, QUOTE] ␈↓αthen
␈↓ ↓H␈↓α␈↓↓class1[␈↓αd ␈↓↓u, [2 . ␈↓αa ␈↓↓u] . v]␈↓ α_␈↓αelse
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *49
␈↓ ↓H␈↓αif ␈↓↓ccchain ␈↓αa ␈↓↓u ␈↓αthen ␈↓↓class1[␈↓αd ␈↓↓u, [3 . ␈↓αa
␈↓ ↓H␈↓α␈↓↓u] . v]␈↓ α_␈↓αelse ␈↓↓class1[␈↓αd ␈↓↓u, [4 . ␈↓αa
␈↓ ↓H␈↓α␈↓↓u] . v]
␈↓ ↓H␈↓↓␈↓ ↓xclass2[u, v, flg] ←␈↓ α_␈↓αif n ␈↓↓u
␈↓ ↓H␈↓↓␈↓αthen ␈↓↓v␈↓ α_␈↓αelse if ␈↓↓flg ∧ ␈↓αaa ␈↓↓u ␈↓αeq 4 then
␈↓ ↓H␈↓α␈↓↓class2[␈↓αd ␈↓↓u, [5 . ␈↓αda ␈↓↓u] . v, NIL]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse ␈↓↓class2[␈↓αd ␈↓↓u, ␈↓αa ␈↓↓u . v, flg]
␈↓ ↓H␈↓↓␈↓ ↓xmkjrst l ← <<JRST, 0, l>>
␈↓ ↓H␈↓↓␈↓ ↓xcombool[p, m, l, flg, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif ␈↓↓p ␈↓αeq T then [if ␈↓↓flg ␈↓αthen ␈↓↓mkjrst l ␈↓αelse NIL]
␈↓ ↓H␈↓α␈↓ α_else if at ␈↓↓p ␈↓αthen <␈↓↓compexp[p, m, vpr], <<␈↓αif ␈↓↓flg ␈↓αthen JUMPN else JUMPE, 1, ␈↓↓l>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq EQ then
␈↓ ↓H␈↓α␈↓ α8<␈↓↓complisa[␈↓αd ␈↓↓p, m, vpr], ␈↓αif ␈↓↓flg ␈↓αthen ((CAMN 1 2)) else ((CAME 1 2)), ␈↓↓mkjrst l>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq AND then
␈↓ ↓H␈↓α␈↓ α8[if ¬␈↓↓flg ␈↓αthen ␈↓↓compandor[␈↓αd ␈↓↓p, m, l, NIL, vpr]
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse {␈↓↓gensym[]}[λl1. <compandor1[␈↓αd ␈↓↓p, m, l1, l, NIL, vpr], <<LABEL, l1>>>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq OR then
␈↓ ↓H␈↓α␈↓ α8[if ␈↓↓flg ␈↓αthen ␈↓↓compandor[␈↓αd ␈↓↓p, m, l, T, vpr]
␈↓ ↓H␈↓↓␈↓ αA␈↓αelse {␈↓↓gensym[]}[λl1. <compandor1[␈↓αd ␈↓↓p, m, l1, l, T, vpr], <<LABEL, l1>>>]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq NOT then ␈↓↓combool[␈↓αad ␈↓↓p, m, l, ¬flg, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓p ␈↓αeq NULL then
␈↓ ↓H␈↓α␈↓ α8<␈↓↓compexp[␈↓αad ␈↓↓p, m, vpr], <<␈↓αif ␈↓↓flg ␈↓αthen JUMPE else JUMPN, 1, ␈↓↓l>>>
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse <␈↓↓compexp[p, m, vpr], <<␈↓αif ␈↓↓flg ␈↓αthen JUMPN else JUMPE, 1, ␈↓↓l>>>
␈↓ ↓H␈↓↓␈↓ ↓xcompandor[u, m, l, flg, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen NIL else <␈↓↓combool[␈↓αa ␈↓↓u, m, l, flg, vpr], compandor[␈↓αd ␈↓↓u, m, l, flg, vpr]>
␈↓ ↓H␈↓↓␈↓ ↓xcompandor1[u, m, l, l2, flg, vpr] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓mkjrst l2
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if n d ␈↓↓u ␈↓αthen ␈↓↓combool[␈↓αa ␈↓↓u, m, l2, ¬flg, vpr]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse <␈↓↓combool[␈↓αa ␈↓↓u, m, l, flg, vpr], compandor1[␈↓αd ␈↓↓u, m, l, l2, flg, vpr]>
␈↓ ↓H␈↓↓␈↓ ↓xflat[u, s] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓s
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if n a ␈↓↓u ␈↓αthen ␈↓↓flat[␈↓αd ␈↓↓u, s]
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *50
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if a ␈↓↓u ␈↓αeq LABEL then ad ␈↓↓u . s
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a ␈↓↓u ␈↓αthen ␈↓↓u . s
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse ␈↓↓flat[␈↓αa ␈↓↓u, flat[␈↓αd ␈↓↓u, s]]
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *51
␈↓ ↓H␈↓Annotated listing of LCOM0:
␈↓ ↓H␈↓(DEFPROP LC0FNS
␈↓ ↓H␈↓ (LC0FNS COMPL
␈↓ ↓H␈↓ COMP
␈↓ ↓H␈↓ PRUP
␈↓ ↓H␈↓ MKPUSH
␈↓ ↓H␈↓ COMPEXP
␈↓ ↓H␈↓ COMPLIS
␈↓ ↓H␈↓ LOADAC
␈↓ ↓H␈↓ COMCOND
␈↓ ↓H␈↓ COMBOOL
␈↓ ↓H␈↓ COMPANDOR)
␈↓ ↓H␈↓VALUE)
␈↓ ↓H␈↓~COMPL is the user-callable driver. It is a FEXPR. It takes as
␈↓ ↓H␈↓~ an argument a single file name, which must have no extension.
␈↓ ↓H␈↓~ EXPRs on a file called FILNAM will be compiled into LAP and
␈↓ ↓H␈↓~ written on the file FILNAM.LAP. Other types of function
␈↓ ↓H␈↓~ definitions and non-definitions are simply copied to output.
␈↓ ↓H␈↓(DEFPROP COMPL
␈↓ ↓H␈↓ (LAMBDA(FILE)
␈↓ ↓H␈↓ (PROG (Z)
␈↓ ↓H␈↓ (EVAL
␈↓ ↓H␈↓ (CONS (QUOTE OUTPUT)
␈↓ ↓H␈↓ (CONS (QUOTE DSK:)
␈↓ ↓H␈↓ (LIST (CONS (CAR FILE) (QUOTE LAP))))))
␈↓ ↓H␈↓ (EVAL (CONS (QUOTE INPUT) (CONS (QUOTE DSK:) FILE)))
␈↓ ↓H␈↓ (INC T NIL)
␈↓ ↓H␈↓ LOOP (SETQ Z (ERRSET (READ)))
␈↓ ↓H␈↓ (COND ((ATOM Z) (GO DONE)))
␈↓ ↓H␈↓ (SETQ Z (CAR Z))
␈↓ ↓H␈↓ (COND ((OR (EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (AND (EQ (CAR Z) (QUOTE DEFPROP))
␈↓ ↓H␈↓ (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ (PROG (PROG)
␈↓ ↓H␈↓ (SETQ PROG
␈↓ ↓H␈↓ (COND ((EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADDR Z)
␈↓ ↓H␈↓ (CADDDR Z)))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADR (CADDR Z))
␈↓ ↓H␈↓ (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ (OUTC T NIL)
␈↓ ↓H␈↓ (MAPC (FUNCTION PRINT) PROG)
␈↓ ↓H␈↓ (OUTC NIL NIL)
␈↓ ↓H␈↓ (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ (T (OUTC T NIL) (PRINT Z) (OUTC NIL NIL)))
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *52
␈↓ ↓H␈↓ (GO LOOP)
␈↓ ↓H␈↓ DONE (OUTC T NIL)
␈↓ ↓H␈↓ (OUTC NIL T)
␈↓ ↓H␈↓ (INC NIL T)
␈↓ ↓H␈↓ (RETURN (QUOTE ENDCOMP))))
␈↓ ↓H␈↓FEXPR)
␈↓ ↓H␈↓~COMP compiles a single function definition, returning a list of
␈↓ ↓H␈↓~ the LAP code corresponding to the definition.
␈↓ ↓H␈↓~ FN is the atomic name of the function being compiled.
␈↓ ↓H␈↓~ VARS is the formal parameter list for the function.
␈↓ ↓H␈↓~ EXP is the function body.
␈↓ ↓H␈↓(DEFPROP COMP
␈↓ ↓H␈↓ (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ (MKPUSH N 1)
␈↓ ↓H␈↓ (COMPEXP EXP (MINUS N) (PRUP VARS 1))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))
␈↓ ↓H␈↓ (QUOTE ((POPJ P) NIL))))
␈↓ ↓H␈↓ (LENGTH VARS)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~PRUP returns an A-LIST formed by pairing successive elements of
␈↓ ↓H␈↓~ VARS with consecutive integers beginning with N.
␈↓ ↓H␈↓(DEFPROP PRUP
␈↓ ↓H␈↓ (LAMBDA(VARS N)
␈↓ ↓H␈↓ (COND ((NULL VARS) NIL)
␈↓ ↓H␈↓ (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (PLUS N 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~MKPUSH returns a list of N (PUSH P i) instructions, where i runs
␈↓ ↓H␈↓~ from M to M+N-1. Used to push arguments onto the stack.
␈↓ ↓H␈↓(DEFPROP MKPUSH
␈↓ ↓H␈↓ (LAMBDA(N M)
␈↓ ↓H␈↓ (COND ((LESSP N M) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE PUSH) (QUOTE P) M)
␈↓ ↓H␈↓ (MKPUSH N (PLUS M 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPEXP is the heart of LCOM0. It determines precisely
␈↓ ↓H␈↓~ what an expression is, and compiles appropriate code
␈↓ ↓H␈↓~ for it. It returns a list of that code.
␈↓ ↓H␈↓~ EXP is the expression to be compiled.
␈↓ ↓H␈↓~ M is minus the number of entries on the stack. When
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *53
␈↓ ↓H␈↓~ added to a value retrieved from the A-LIST VPR, it
␈↓ ↓H␈↓~ can be used to locate a variable on the stack.
␈↓ ↓H␈↓~ VPR is an A-LIST, associating variable names with
␈↓ ↓H␈↓~ numbers which, when added to M, give stack offsets.
␈↓ ↓H␈↓~ Both M and VPR maintain these definitions throughout.
␈↓ ↓H␈↓(DEFPROP COMPEXP
␈↓ ↓H␈↓ (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ (COND ((NULL EXP) (QUOTE ((MOVEI 1 0)))) ~NIL
␈↓ ↓H␈↓ ((EQ EXP T) (QUOTE ((MOVEI 1 (QUOTE T))))) ~T
␈↓ ↓H␈↓ ((ATOM EXP) ~variable
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC EXP VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ ((OR (EQ (CAR EXP) (QUOTE AND)) ~boolean expression
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE NOT)))
␈↓ ↓H␈↓ ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ L1
␈↓ ↓H␈↓ (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ L2)))
␈↓ ↓H␈↓ (GENSYM)
␈↓ ↓H␈↓ (GENSYM)))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE COND)) ~COND
␈↓ ↓H␈↓ (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE QUOTE)) ~QUOTE
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ ((ATOM (CAR EXP)) ~function call
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ (LOADAC (DIFFERENCE 1 N) 1)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE CALL)
␈↓ ↓H␈↓ N
␈↓ ↓H␈↓ (LIST (QUOTE E) (CAR EXP))
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *54
␈↓ ↓H␈↓ (QUOTE S)))))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ ((EQ (CAAR EXP) (QUOTE LAMBDA)) ~LAMBDA expression
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ (COMPEXP
␈↓ ↓H␈↓ (CADDAR EXP)
␈↓ ↓H␈↓ (DIFFERENCE M N)
␈↓ ↓H␈↓ (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0)))))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ (T NIL))) ~oops
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPLIS compiles code to evaluate each expression in a list of
␈↓ ↓H␈↓~ expressions and to push those values onto the stack. It
␈↓ ↓H␈↓~ returns a list of that code. It is used to compile code
␈↓ ↓H␈↓~ to evaluate arguments to called functions or LAMBDA expressions.
␈↓ ↓H␈↓~ U is a list of expressions.
␈↓ ↓H␈↓(DEFPROP COMPLIS
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (APPEND (COMPEXP (CAR U) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (COMPLIS (CDR U) (DIFFERENCE M 1) VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~LOADAC returns a list of (MOVE i j P) instructions, loading
␈↓ ↓H␈↓~ consecutive accumulators from the top of the stack.
␈↓ ↓H␈↓~ K indexes the accumulator loaded.
␈↓ ↓H␈↓~ N indexes the stack offset.
␈↓ ↓H␈↓(DEFPROP LOADAC
␈↓ ↓H␈↓ (LAMBDA(N K)
␈↓ ↓H␈↓ (COND ((GREATERP N 0) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE) K N (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (PLUS N 1) (PLUS K 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMCOND compiles a COND.
␈↓ ↓H␈↓~ U is a list of clauses in the COND.
␈↓ ↓H␈↓~ L is a label to be emitted at the end of all code for
␈↓ ↓H␈↓~ the COND.
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *55
␈↓ ↓H␈↓(DEFPROP COMCOND
␈↓ ↓H␈↓ (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (LIST L))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) L) L1)
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMBOOL compiles code for a single predicate. That is, the
␈↓ ↓H␈↓~ code generated evaluates the predicate and branches somewhere,
␈↓ ↓H␈↓~ depending on the value.
␈↓ ↓H␈↓~ P is the predicate.
␈↓ ↓H␈↓~ L is a label which represents the branch point.
␈↓ ↓H␈↓~ FLG is a flag. If FLG is NIL, code is to fall thru on non-NIL
␈↓ ↓H␈↓~ result and branch to L on NIL result. If FLG is non-NIL,
␈↓ ↓H␈↓~ code is to fall thru on NIL result and branch to L on
␈↓ ↓H␈↓~ non-NIL result.
␈↓ ↓H␈↓(DEFPROP COMBOOL
␈↓ ↓H␈↓ (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ (COND ((ATOM P) ~simple variable
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE AND)) ~conjunction
␈↓ ↓H␈↓ (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMPANDOR (CDR P) M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L))
␈↓ ↓H␈↓ (LIST L1)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE OR)) ~disjunction
␈↓ ↓H␈↓ (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (APPEND (COMPANDOR (CDR P) M L1 T VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L))
␈↓ ↓H␈↓ (LIST L1)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NOT)) ~negation
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *56
␈↓ ↓H␈↓ (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ (T ~other expression
␈↓ ↓H␈↓ (APPEND
␈↓ ↓H␈↓ (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓~COMPANDOR compiles code for lists of predicates connected
␈↓ ↓H␈↓~ conjunctively or disjunctively.
␈↓ ↓H␈↓~ U is a list of predicates.
␈↓ ↓H␈↓~ L is a label.
␈↓ ↓H␈↓~ FLG is a flag. If FLG is NIL, we are to fall thru on non-NIL
␈↓ ↓H␈↓~ results and branch to L on NIL results (AND case). If FLG
␈↓ ↓H␈↓~ is non-NIL, we are to fall thru on NIL results and branch
␈↓ ↓H␈↓~ to L on non-NIL results (OR case).
␈↓ ↓H␈↓(DEFPROP COMPANDOR
␈↓ ↓H␈↓ (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (APPEND (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *57
␈↓ ↓H␈↓LCOM4:
␈↓ ↓H␈↓(DEFPROP COMPFCNS
␈↓ ↓H␈↓ (COMPFCNS COMPL
␈↓ ↓H␈↓ COMP
␈↓ ↓H␈↓ SUBSTACK
␈↓ ↓H␈↓ PRUP
␈↓ ↓H␈↓ MKPUSH
␈↓ ↓H␈↓ COMPEXP
␈↓ ↓H␈↓ STACKUP
␈↓ ↓H␈↓ CCCHAIN
␈↓ ↓H␈↓ COMPC
␈↓ ↓H␈↓ COMCOND
␈↓ ↓H␈↓ COMPLISA
␈↓ ↓H␈↓ CCOUNT
␈↓ ↓H␈↓ LOADAC
␈↓ ↓H␈↓ COMPLIS
␈↓ ↓H␈↓ CLASSIFY
␈↓ ↓H␈↓ CLASS1
␈↓ ↓H␈↓ CLASS2
␈↓ ↓H␈↓ MKJRST
␈↓ ↓H␈↓ COMBOOL
␈↓ ↓H␈↓ COMPANDOR
␈↓ ↓H␈↓ COMPANDOR1
␈↓ ↓H␈↓ FLAT)
␈↓ ↓H␈↓VALUE)
␈↓ ↓H␈↓(DEFPROP COMPL
␈↓ ↓H␈↓ (LAMBDA(FILE)
␈↓ ↓H␈↓ (PROG (Z)
␈↓ ↓H␈↓ (EVAL
␈↓ ↓H␈↓ (CONS (QUOTE OUTPUT)
␈↓ ↓H␈↓ (CONS (QUOTE DSK:)
␈↓ ↓H␈↓ (LIST (CONS (CAR FILE) (QUOTE LAP))))))
␈↓ ↓H␈↓ (EVAL (CONS (QUOTE INPUT) (CONS (QUOTE DSK:) FILE)))
␈↓ ↓H␈↓ (INC (QUOTE T) NIL)
␈↓ ↓H␈↓ (OUTC T NIL)
␈↓ ↓H␈↓ LOOP (SETQ Z (ERRSET (READ)))
␈↓ ↓H␈↓ (COND ((ATOM Z) (GO DONE)) ((QUOTE T) (QUOTE NIL)))
␈↓ ↓H␈↓ (SETQ Z (CAR Z))
␈↓ ↓H␈↓ (COND ((OR (EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (AND (EQ (CAR Z) (QUOTE DEFPROP))
␈↓ ↓H␈↓ (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ (PROG (PROG)
␈↓ ↓H␈↓ (SETQ PROG
␈↓ ↓H␈↓ (COND ((EQ (CAR Z) (QUOTE DE))
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADDR Z)
␈↓ ↓H␈↓ (CADDDR Z)))
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *58
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (COMP (CADR Z)
␈↓ ↓H␈↓ (CADR (CADDR Z))
␈↓ ↓H␈↓ (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ (MAPC (FUNCTION PRINT) PROG)
␈↓ ↓H␈↓ (OUTC NIL NIL)
␈↓ ↓H␈↓ (PRINT (LIST (CADR Z) (LENGTH PROG)))
␈↓ ↓H␈↓ (OUTC T NIL)))
␈↓ ↓H␈↓ (T (PRINT Z)))
␈↓ ↓H␈↓ (GO LOOP)
␈↓ ↓H␈↓ DONE (OUTC NIL T)
␈↓ ↓H␈↓ (INC NIL T)
␈↓ ↓H␈↓ (RETURN (QUOTE ENDCOMP))))
␈↓ ↓H␈↓FEXPR)
␈↓ ↓H␈↓(DEFPROP COMP
␈↓ ↓H␈↓ (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ ((LAMBDA(VPR N)
␈↓ ↓H␈↓ (FLAT (LIST (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ (MKPUSH N 1)
␈↓ ↓H␈↓ (COMPEXP EXP (MINUS N) VPR)
␈↓ ↓H␈↓ (SUBSTACK N)
␈↓ ↓H␈↓ (QUOTE ((POPJ P) (LABEL NIL))))
␈↓ ↓H␈↓ NIL))
␈↓ ↓H␈↓ (PRUP VARS 1)
␈↓ ↓H␈↓ (LENGTH VARS)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP SUBSTACK
␈↓ ↓H␈↓ (LAMBDA(N)
␈↓ ↓H␈↓ (COND ((EQ N 0) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE C) N 0 N 0))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP PRUP
␈↓ ↓H␈↓ (LAMBDA(VARS N)
␈↓ ↓H␈↓ (COND ((NULL VARS) NIL)
␈↓ ↓H␈↓ (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (PLUS N 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP MKPUSH
␈↓ ↓H␈↓ (LAMBDA(N M)
␈↓ ↓H␈↓ (COND ((LESSP N M) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE PUSH) (QUOTE P) M)
␈↓ ↓H␈↓ (MKPUSH N (PLUS M 1))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *59
␈↓ ↓H␈↓(DEFPROP COMPEXP
␈↓ ↓H␈↓ (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ ((OR (EQ EXP (QUOTE T)) (NUMBERP EXP))
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ ((ATOM EXP)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC EXP VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HLRZ@)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR EXP) M VPR)
␈↓ ↓H␈↓ (QUOTE ((HLRZ@ 1 1)))))))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CDR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HRRZ@)
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR EXP) M VPR)
␈↓ ↓H␈↓ (QUOTE ((HRRZ@ 1 1)))))))
␈↓ ↓H␈↓ ((OR (EQ (CAR EXP) (QUOTE AND))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE NOT))
␈↓ ↓H␈↓ (EQ (CAR EXP) (QUOTE EQ)))
␈↓ ↓H␈↓ ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ (LIST (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L1)
␈↓ ↓H␈↓ (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L2))))
␈↓ ↓H␈↓ (GENSYM)
␈↓ ↓H␈↓ (GENSYM)))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE COND))
␈↓ ↓H␈↓ (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE QUOTE))
␈↓ ↓H␈↓ (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ ((ATOM (CAR EXP))
␈↓ ↓H␈↓ (LIST (COMPLISA (CDR EXP) M VPR)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *60
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE CALL)
␈↓ ↓H␈↓ (LENGTH (CDR EXP))
␈↓ ↓H␈↓ (LIST (QUOTE E) (CAR EXP))
␈↓ ↓H␈↓ (QUOTE S)))))
␈↓ ↓H␈↓ ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ ((LAMBDA(N)
␈↓ ↓H␈↓ (LIST (STACKUP (CDR EXP) M VPR)
␈↓ ↓H␈↓ (COMPEXP
␈↓ ↓H␈↓ (CADDAR EXP)
␈↓ ↓H␈↓ (DIFFERENCE M N)
␈↓ ↓H␈↓ (APEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ (SUBSTACK N)))
␈↓ ↓H␈↓ (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ ((QUOTE T) (QUOTE NIL))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP STACKUP
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP (CAR U) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (STACKUP (CDR U) (DIFFERENCE M 1) VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CCCHAIN
␈↓ ↓H␈↓ (LAMBDA(EXP)
␈↓ ↓H␈↓ (AND (OR (EQ (CAR EXP) (QUOTE CAR)) (EQ (CAR EXP) (QUOTE CDR)))
␈↓ ↓H␈↓ (OR (ATOM (CADR EXP)) (CCCHAIN (CADR EXP)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPC
␈↓ ↓H␈↓ (LAMBDA(EXP N2 M VPR)
␈↓ ↓H␈↓ (COND ((ATOM EXP) (ERR (QUOTE COMPC)))
␈↓ ↓H␈↓ ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HLRZ@)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE HLRZ@) N2 N2)
␈↓ ↓H␈↓ (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓ ((ATOM (CADR EXP))
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (QUOTE HRRZ@)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CADR EXP) VPR)))
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *61
␈↓ ↓H␈↓ (QUOTE P))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE HRRZ@) N2 N2)
␈↓ ↓H␈↓ (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMCOND
␈↓ ↓H␈↓ (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (LIST (LIST (QUOTE LABEL) L)))
␈↓ ↓H␈↓ ((AND (NOT (ATOM (CAAR U)))
␈↓ ↓H␈↓ (EQ (CAAAR U) (QUOTE NULL))
␈↓ ↓H␈↓ (NULL (CADAR U)))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADAAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JUMPE) 1 L))
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR U) (QUOTE T))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (LIST (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE JRST) 0 L)
␈↓ ↓H␈↓ (LIST (QUOTE LABEL) L1))
␈↓ ↓H␈↓ (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPLISA
␈↓ ↓H␈↓ (LAMBDA(U M VPR)
␈↓ ↓H␈↓ ((LAMBDA(Z)
␈↓ ↓H␈↓ (LIST (COMPLIS Z M 1 VPR)
␈↓ ↓H␈↓ (LOADAC Z
␈↓ ↓H␈↓ (DIFFERENCE 1 (CCOUNT Z))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ (DIFFERENCE M (CCOUNT Z))
␈↓ ↓H␈↓ VPR)
␈↓ ↓H␈↓ (SUBSTACK (CCOUNT Z))))
␈↓ ↓H␈↓ (CLASSIFY U)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CCOUNT
␈↓ ↓H␈↓ (LAMBDA(Z)
␈↓ ↓H␈↓ (COND ((NULL Z) 0)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 4) (PLUS 1 (CCOUNT (CDR Z))))
␈↓ ↓H␈↓ (T (CCOUNT (CDR Z)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP LOADAC
␈↓ ↓H␈↓ (LAMBDA(Z M2 N2 M VPR)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *62
␈↓ ↓H␈↓ (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 1)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE)
␈↓ ↓H␈↓ N2
␈↓ ↓H␈↓ (PLUS M (CDR (ASSOC (CDAR Z) VPR)))
␈↓ ↓H␈↓ (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 0)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVEI) N2 (LIST (QUOTE QUOTE) (CDAR Z)))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 2)
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVEI) N2 (CDAR Z))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 3)
␈↓ ↓H␈↓ (LIST (REVERSE (COMPC (CDAR Z) N2 M VPR))
␈↓ ↓H␈↓ (LOADAC (CDR Z) M2 (PLUS N2 1) M VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 5) (LOADAC (CDR Z) 1 (PLUS N2 1) M VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (CONS (LIST (QUOTE MOVE) N2 M2 (QUOTE P))
␈↓ ↓H␈↓ (LOADAC (CDR Z) (PLUS M2 1) (PLUS N2 1) M VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPLIS
␈↓ ↓H␈↓ (LAMBDA(Z M K VPR)
␈↓ ↓H␈↓ (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ ((EQ (CAAR Z) 4)
␈↓ ↓H␈↓ (LIST (COMPEXP (CDAR Z) M VPR)
␈↓ ↓H␈↓ (QUOTE ((PUSH P 1)))
␈↓ ↓H␈↓ (COMPLIS (CDR Z) (DIFFERENCE M 1) (PLUS K 1) VPR)))
␈↓ ↓H␈↓ ((EQ (CAAR Z) 5)
␈↓ ↓H␈↓ (LIST (COMPEXP (CDAR Z) M VPR)
␈↓ ↓H␈↓ (COND ((EQ K 1) NIL)
␈↓ ↓H␈↓ (T (LIST (LIST (QUOTE MOVE) K 1))))))
␈↓ ↓H␈↓ (T (COMPLIS (CDR Z) M (PLUS K 1) VPR))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASSIFY
␈↓ ↓H␈↓ (LAMBDA (U) (CLASS2 (CLASS1 U NIL) NIL T))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASS1
␈↓ ↓H␈↓ (LAMBDA(U V)
␈↓ ↓H␈↓ (COND ((NULL U) V)
␈↓ ↓H␈↓ ((ATOM (CAR U))
␈↓ ↓H␈↓ (COND ((OR (EQUAL (CAR U) (QUOTE NIL))
␈↓ ↓H␈↓ (EQUAL (CAR U) (QUOTE T))
␈↓ ↓H␈↓ (NUMBERP (CAR U)))
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 0 (CAR U)) V)))
␈↓ ↓H␈↓ (T (CLASS1 (CDR U) (CONS (CONS 1 (CAR U)) V)))))
␈↓ ↓H␈↓ ((EQUAL (CAAR U) (QUOTE QUOTE))
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *63
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 2 (CAR U)) V)))
␈↓ ↓H␈↓ ((CCCHAIN (CAR U))
␈↓ ↓H␈↓ (CLASS1 (CDR U) (CONS (CONS 3 (CAR U)) V)))
␈↓ ↓H␈↓ (T (CLASS1 (CDR U) (CONS (CONS 4 (CAR U)) V)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP CLASS2
␈↓ ↓H␈↓ (LAMBDA(U V FLG)
␈↓ ↓H␈↓ (COND ((NULL U) V)
␈↓ ↓H␈↓ ((AND FLG (EQ (CAAR U) 4))
␈↓ ↓H␈↓ (CLASS2 (CDR U) (CONS (CONS 5 (CDAR U)) V) NIL))
␈↓ ↓H␈↓ (T (CLASS2 (CDR U) (CONS (CAR U) V) FLG))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP MKJRST
␈↓ ↓H␈↓ (LAMBDA (L) (LIST (LIST (QUOTE JRST) 0 L)))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMBOOL
␈↓ ↓H␈↓ (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ (COND ((EQ P (QUOTE T)) (COND (FLG (MKJRST L)) (T NIL)))
␈↓ ↓H␈↓ ((ATOM P)
␈↓ ↓H␈↓ (LIST (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE EQ))
␈↓ ↓H␈↓ (LIST (COMPLISA (CDR P) M VPR)
␈↓ ↓H␈↓ (COND (FLG (QUOTE ((CAMN 1 2))))
␈↓ ↓H␈↓ (T (QUOTE ((CAME 1 2)))))
␈↓ ↓H␈↓ (MKJRST L)))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (LIST (COMPANDOR1 (CDR P) M L1 L NIL VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ ((LAMBDA(L1)
␈↓ ↓H␈↓ (LIST (COMPANDOR1 (CDR P) M L1 L T VPR)
␈↓ ↓H␈↓ (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ (GENSYM)))))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NOT))
␈↓ ↓H␈↓ (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ ((EQ (CAR P) (QUOTE NULL))
␈↓ ↓H␈↓ (LIST (COMPEXP (CADR P) M VPR)
␈↓ ↓H␈↓␈↓ ¬dCHAPTER III␈↓ *64
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPE)) (T (QUOTE JUMPN)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMPEXP P M VPR)
␈↓ ↓H␈↓ (LIST
␈↓ ↓H␈↓ (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE)))
␈↓ ↓H␈↓ 1
␈↓ ↓H␈↓ L))))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPANDOR
␈↓ ↓H␈↓ (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) NIL)
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP COMPANDOR1
␈↓ ↓H␈↓ (LAMBDA(U M L L2 FLG VPR)
␈↓ ↓H␈↓ (COND ((NULL U) (MKJRST L2))
␈↓ ↓H␈↓ ((NULL (CDR U)) (COMBOOL (CAR U) M L2 (NOT FLG) VPR))
␈↓ ↓H␈↓ (T
␈↓ ↓H␈↓ (LIST (COMBOOL (CAR U) M L FLG VPR)
␈↓ ↓H␈↓ (COMPANDOR1 (CDR U) M L L2 FLG VPR)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓(DEFPROP FLAT
␈↓ ↓H␈↓ (LAMBDA(U S)
␈↓ ↓H␈↓ (COND ((NULL U) S)
␈↓ ↓H␈↓ ((NULL (CAR U)) (FLAT (CDR U) S))
␈↓ ↓H␈↓ ((EQ (CAR U) (QUOTE LABEL)) (CONS (CADR U) S))
␈↓ ↓H␈↓ ((ATOM (CAR U)) (CONS U S))
␈↓ ↓H␈↓ (T (FLAT (CAR U) (FLAT (CDR U) S)))))
␈↓ ↓H␈↓EXPR)
␈↓ ↓H␈↓␈↓ εH␈↓ *65
␈↓ ↓H␈↓α␈↓ ¬{Chapter IV
␈↓ ↓H␈↓α␈↓ ¬BCOMPUTABILITY
␈↓ ↓H␈↓1. ␈↓αThe function ␈↓↓eval␈↓.
␈↓ ↓H␈↓␈↓ α_Except␈αfor␈αspeed␈αand␈αmemory␈αsize␈αall␈αpresent␈αday␈αstored␈αprogram␈αcomputers␈αare␈αequivalent
␈↓ ↓H␈↓in␈αwhat␈αcomputations␈αthey␈αcan␈αdo.␈α A␈αprogram␈αwritten␈αfor␈αone␈αcomputer␈αcan␈αbe␈αtranslated␈αto␈αrun
␈↓ ↓H␈↓on␈αanother.␈α Indeed,␈αone␈αcan␈αwrite␈αa␈αsimulator␈αfor␈αone␈αcomputer␈αto␈αrun␈αon␈αanother.␈α To␈αput␈αit␈αin
␈↓ ↓H␈↓commercial␈α
terms,␈α
no␈α
computer␈α
manufacturer␈α
can␈α
advertise␈α
that␈α
his␈α
machine␈α
can␈α
do␈αcalculations
␈↓ ↓H␈↓impossible on the machine made by his competitors.
␈↓ ↓H␈↓␈↓ α_This␈αis␈αwell␈αknown␈αintuitively,␈αand␈αthe␈αfirst␈αmathematical␈αtheorem␈αof␈αthis␈αkind␈αwas␈αproved
␈↓ ↓H␈↓by␈αA.M.␈αTuring␈α(1936),␈αwho␈αdefined␈αa␈αprimitive␈αkind␈αof␈αcomputer␈αnow␈αcalled␈αa␈αTuring␈αmachine,
␈↓ ↓H␈↓and␈α⊃showed␈α⊃how␈α⊃to␈α⊃make␈α⊂a␈α⊃universal␈α⊃machine␈α⊃that␈α⊃could␈α⊂do␈α⊃any␈α⊃computation␈α⊃done␈α⊃by␈α⊂any
␈↓ ↓H␈↓Turing␈αmachine␈αwhen␈αgiven␈αa␈αdescription␈αof␈αthe␈αmachine␈αto␈αbe␈αsimulated␈αand␈αthe␈αinitial␈αtape␈αof
␈↓ ↓H␈↓the computation to be imitated.
␈↓ ↓H␈↓␈↓ α_In␈α
LISP␈α
the␈αfunction␈α
␈↓↓eval␈↓␈α
is␈α
a␈αuniversal␈α
LISP␈α
function␈α
in␈αthe␈α
sense␈α
that␈α
any␈αcomputation
␈↓ ↓H␈↓done by any LISP function can be done by ␈↓↓eval␈↓ when ␈↓↓eval␈↓ is given suitable arguments.
␈↓ ↓H␈↓␈↓ α_␈↓↓eval␈↓␈αhas␈αtwo␈αarguments␈αthe␈αfirst␈αof␈αwhich␈α
is␈αa␈αLISP␈αexpression␈αin␈αthe␈αnotation␈αgiven␈αin␈α
the
␈↓ ↓H␈↓previous␈αsection,␈αwhile␈αthe␈αsecond␈αis␈αa␈αlist␈αof␈αpairs␈αthat␈αgive␈αthe␈αvalues␈αof␈αany␈αfree␈αvariables␈αthat
␈↓ ↓H␈↓may␈α
occur␈αin␈α
the␈αexpression.␈α
Since␈αany␈α
computation␈α
can␈αbe␈α
described␈αas␈α
evaluating␈αan␈α
expression
␈↓ ↓H␈↓without␈αfree␈α
variables,␈αthe␈α
second␈αargument␈α
plays␈αa␈α
role␈αmainly␈α
in␈αthe␈α
recursive␈αdefinition␈αof␈α
␈↓↓eval␈↓,
␈↓ ↓H␈↓and we can start our computations with the second argument NIL.
␈↓ ↓H␈↓␈↓ α_To␈αillustrate␈αthis,␈αsuppose␈αwe␈αwant␈αto␈αapply␈αthe␈αfunction␈α␈↓↓alt␈↓␈αto␈αthe␈αlist␈α(A␈αB␈αC␈αD␈αE),␈αi.e.␈αwe
␈↓ ↓H␈↓wish to evaluate ␈↓↓alt[(A B C D E)]␈↓. This can be obtained by computing
␈↓ ↓H␈↓␈↓ α_␈↓↓eval[((LABEL ALT (LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)
␈↓ ↓H␈↓(T (CONS (CAR X) (ALT (CDDR X))))))) (QUOTE (A B C D E)), NIL],
␈↓ ↓H␈↓and␈αgives␈αthe␈αexpected␈αresult␈α(A␈αC␈αE).␈α The␈αsecond␈αargument␈αof␈α␈↓↓eval␈↓,␈αtaken␈αas␈αNIL␈αin␈αthe␈αabove
␈↓ ↓H␈↓example␈α
is␈α∞a␈α
list␈α
of␈α∞dotted␈α
pairs␈α
where␈α∞the␈α
first␈α
element␈α∞of␈α
each␈α
pair␈α∞is␈α
an␈α
atom␈α∞representing␈α
a
␈↓ ↓H␈↓variable␈α∞and␈α∂the␈α∞second␈α∂element␈α∞is␈α∂the␈α∞value␈α∂assigned␈α∞to␈α∂that␈α∞variable.␈α∂ A␈α∞variable␈α∂may␈α∞occur
␈↓ ↓H␈↓more␈α∞than␈α∞once␈α∞in␈α
the␈α∞list␈α∞and␈α∞the␈α∞value␈α
chosen␈α∞is␈α∞that␈α∞paired␈α
with␈α∞the␈α∞first␈α∞occurrence␈α∞of␈α
the
␈↓ ↓H␈↓variable. We illustrate this by the equation
␈↓ ↓H␈↓␈↓ α_␈↓↓eval[(CAR X), ((X.(B.C)) (Y.A) (X.B))] = B␈↓,
␈↓ ↓H␈↓i.e.␈αwe␈αhave␈αevaluated␈α␈↓αa␈α␈↓↓x␈↓␈αwith␈α␈↓↓␈αx␈α=␈α(B.C)␈↓.␈α The␈αvalue␈αassociated␈αwith␈αa␈αvariable␈αin␈αsuch␈αa␈αlist␈α
of
␈↓ ↓H␈↓pairs is computed by the auxiliary function ␈↓↓assoc␈↓ which has the recursive definition
␈↓ ↓H␈↓␈↓ ¬cCHAPTER IV␈↓ *66
␈↓ ↓H␈↓␈↓ α_␈↓↓assoc[v, a] ← ␈↓αif n ␈↓↓a ␈↓αthen NIL else if aa ␈↓↓a ␈↓α eq ␈↓↓v ␈↓αthen a ␈↓↓a ␈↓αelse ␈↓↓alt[v, ␈↓αd ␈↓↓a]␈↓.
␈↓ ↓H␈↓␈↓ α_Thus we have ␈↓↓assoc[X, ((X.(B.C)) (Y.A) (X.B))] = (X.(B.C))␈↓.
␈↓ ↓H␈↓␈↓ α_A simplified version of the usual LISP ␈↓↓eval␈↓ is the following:
␈↓ ↓H␈↓␈↓ α_␈↓↓eval[e, a] ←
␈↓ ↓H␈↓↓␈↓ α_␈↓αif at ␈↓↓e ␈↓αthen [if ␈↓↓numberp e ∨ e ␈↓αeq NIL ∨ ␈↓↓e ␈↓αeq T then ␈↓↓e ␈↓αelse␈↓↓ assoc[e, a]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a␈↓↓ e ␈↓αthen
␈↓ ↓H␈↓↓␈↓ α_ [␈↓αif a ␈↓↓e␈↓α eq CAR then a ␈↓↓eval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq CDR then d ␈↓↓eval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq CONS then ␈↓↓eval[␈↓αad ␈↓↓e, a] . eval[␈↓αadd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq ATOM then at ␈↓↓eval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq EQ then at ␈↓↓eval[␈↓αad ␈↓↓e, a] ␈↓αeq ␈↓↓eval[␈↓αadd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq QUOTE then ad ␈↓↓e
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq COND then ␈↓↓evcon[␈↓αd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e␈↓α eq LIST then ␈↓↓mapcar[␈↓αd ␈↓↓e, λx: eval[x, a]]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse ␈↓↓eval[␈↓αd ␈↓↓assoc[␈↓αa ␈↓↓e, a] . ␈↓αd ␈↓↓e, a]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓e ␈↓αeq LAMBDA then ␈↓↓eval[␈↓αadda ␈↓↓e, prup[␈↓αada ␈↓↓e, mapcar[␈↓αd ␈↓↓e, λx: eval[x, a]]] * a]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓e ␈↓αeq LABEL then ␈↓↓eval[␈↓αadda ␈↓↓e . ␈↓αd ␈↓↓e, [␈↓αada ␈↓↓e . ␈↓αa ␈↓↓e] . a]␈↓,
␈↓ ↓H␈↓↓␈↓ α_where the auxiliary function evcon␈↓ is defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓evcon[u, a] ← ␈↓αif ␈↓↓eval[␈↓αaa ␈↓↓u, a] ␈↓αthen ␈↓↓eval[␈↓αada ␈↓↓u, a] ␈↓αelse ␈↓↓evcon[␈↓αd ␈↓↓u, a]␈↓,
␈↓ ↓H␈↓and␈α
the␈α
auxiliary␈αfunction␈α
␈↓↓prup␈↓␈α
used␈αfor␈α
pairing␈α
up␈α
the␈αelements␈α
of␈α
two␈αlists␈α
of␈α
equal␈α
length␈αis
␈↓ ↓H␈↓defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓prup[u, v] ← ␈↓αif n ␈↓↓u ␈↓αthen NIL else [a ␈↓↓u . ␈↓αa ␈↓↓v] . prup[␈↓αd ␈↓↓u,␈↓αd ␈↓↓v].␈↓
␈↓ ↓H␈↓␈↓ α_The␈αway␈α␈↓↓eval␈↓␈αworks␈αshould␈αbe␈αclear;␈αatoms␈αare␈αeither␈αimmediately␈αevaluable␈αor␈αhave␈αto␈αbe
␈↓ ↓H␈↓looked␈α⊃up␈α⊃on␈α⊃the␈α⊃list␈α⊃␈↓↓a␈↓;␈α⊃expressions␈α⊃whose␈α⊃first␈α⊃term␈α⊃is␈α⊃one␈α⊃of␈α⊃the␈α⊃elementary␈α⊃functions␈α⊂are
␈↓ ↓H␈↓evaluated␈α∞by␈α∞performing␈α
the␈α∞indicated␈α∞operation␈α
on␈α∞the␈α∞result␈α
of␈α∞evaluating␈α∞the␈α∞arguments;␈α
␈↓↓list␈↓
␈↓ ↓H␈↓has␈α∞to␈α
be␈α∞handled␈α
specially,␈α∞because␈α
it␈α∞has␈α
an␈α∞indefinite␈α
number␈α∞of␈α
arguments;␈α∞conditionals␈α
are
␈↓ ↓H␈↓handled␈α
by␈α
an␈α
auxiliary␈α
function␈α
that␈αevaluates␈α
the␈α
terms␈α
in␈α
the␈α
right␈α
order;␈αquoted␈α
S-expressions
␈↓ ↓H␈↓are␈α∞trivial;␈α∞non-elementary␈α∞functions␈α∞have␈α∞their␈α∂definitions␈α∞looked␈α∞up␈α∞on␈α∞␈↓↓a␈↓␈α∞and␈α∂substituted␈α∞for
␈↓ ↓H␈↓their␈αnames;␈αwhen␈αa␈αfunction␈αis␈αspecified␈αby␈αa␈αλ,␈αthe␈αinner␈αexpression␈αis␈αevaluated␈αwith␈αa␈α
new␈α␈↓↓a␈↓
␈↓ ↓H␈↓which␈α⊂is␈α⊂obtained␈α⊂by␈α⊃evaluating␈α⊂the␈α⊂arguments␈α⊂and␈α⊂pairing␈α⊃them␈α⊂up␈α⊂with␈α⊂the␈α⊃variables␈α⊂and
␈↓ ↓H␈↓putting␈αthem␈α
on␈αthe␈αfront␈α
of␈αthe␈αold␈α
␈↓↓a␈↓;␈αand␈α
finally,␈α␈↓αlabel␈↓␈αis␈α
handled␈αby␈αpairing␈α
the␈αname␈α
of␈αthe
␈↓ ↓H␈↓function with the expression on ␈↓↓a␈↓ and replacing the whole function by the λ-part.
␈↓ ↓H␈↓␈↓ α_␈↓↓eval␈↓␈αplays␈αboth␈αa␈αtheoretical␈αand␈αa␈αpractical␈αrole␈αin␈αLISP.␈α Historically,␈αthe␈αlist␈αnotation␈αfor
␈↓ ↓H␈↓LISP␈αfunctions␈αand␈α␈↓↓eval␈↓␈α
were␈αfirst␈αdevised␈αin␈α
order␈αto␈αshow␈αhow␈αeasy␈α
it␈αis␈αto␈αdefine␈α
a␈αuniversal
␈↓ ↓H␈↓function␈α
in␈αLISP␈α
-␈αthe␈α
idea␈α
was␈αto␈α
advocate␈αLISP␈α
as␈α
an␈αalternative␈α
to␈αTuring␈α
machines␈αfor␈α
doing
␈↓ ↓H␈↓the␈αelementary␈α
theory␈αof␈α
computability.␈α The␈α
notation␈αused␈α
was␈αchosen␈α
without␈αmuch␈α
regard␈αfor
␈↓ ↓H␈↓human␈αconvenience,␈αbecause␈αthe␈αoriginal␈αidea␈αwas␈αpurely␈αtheoretical;␈αthe␈αnotation␈αfor␈αconditional
␈↓ ↓H␈↓expressions,␈α
for␈α
example,␈α
has␈α
an␈α
unnecessary␈α
extra␈α
level␈α
of␈α
parentheses.␈α
However,␈α
S.␈α∞R.␈α
Russell
␈↓ ↓H␈↓noted␈α
that␈α
␈↓↓eval␈↓␈α
could␈α
serve␈αas␈α
an␈α
interpreter␈α
for␈α
LISP␈αand␈α
promptly␈α
programmed␈α
it␈α
in␈αmachine
␈↓ ↓H␈↓language␈αwith␈αminor␈αmodifications␈αfor␈αpractical␈αpurposes.␈α Since␈αa␈αcompiler␈αwas␈αlong␈αdelayed,␈αthe
␈↓ ↓H␈↓␈↓ ¬cCHAPTER IV␈↓ *67
␈↓ ↓H␈↓interpreter␈αwas␈αmore␈αeasily␈αmodified␈α
and␈αhandled␈αsome␈αdifficult␈αcases␈αwith␈α
functional␈αarguments
␈↓ ↓H␈↓better, an interpreter based on ␈↓↓eval␈↓ has remained a feature of most LISP systems.
␈↓ ↓H␈↓␈↓ α_The␈α⊂way␈α⊂␈↓↓eval␈↓␈α⊂handles␈α⊂arguments␈α∂corresponds␈α⊂to␈α⊂the␈α⊂call-by-value␈α⊂method␈α⊂of␈α∂parameter
␈↓ ↓H␈↓passing␈αin␈αALGOL␈αand␈αsimilar␈αlanguages.␈α There␈αis␈αalso␈αa␈αform␈αof␈α␈↓↓eval␈↓␈αthat␈αcorresponds␈αto␈αcall-
␈↓ ↓H␈↓by-name. Here it is:
␈↓ ↓H␈↓␈↓ α_␈↓↓neval[e, a] ← ␈↓αif at ␈↓↓e␈↓α then
␈↓ ↓H␈↓␈↓ α_ [␈↓αif ␈↓↓e ␈↓αeq T then T
␈↓ ↓H␈↓α␈↓ α_ else if ␈↓↓e ␈↓αeq NIL then NIL
␈↓ ↓H␈↓α␈↓ α_ else if ␈↓↓numberp e ␈↓αthen ␈↓↓e
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse ␈↓↓neval[␈↓αad ␈↓↓assoc[e, a], ␈↓αdd ␈↓↓ assoc[e, a]]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if at a ␈↓↓e ␈↓α then
␈↓ ↓H␈↓↓␈↓ α_ [␈↓αif a ␈↓↓e ␈↓αeq CAR then a ␈↓↓neval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq CDR then d ␈↓↓neval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq CONS then ␈↓↓neval[␈↓αad ␈↓↓e, a] . neval[␈↓αadd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq ATOM then at ␈↓↓neval[␈↓αad ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq EQ then ␈↓↓neval[␈↓αad ␈↓↓e, a] ␈↓αeq ␈↓↓neval[␈↓αadd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq QUOTE then ad ␈↓↓e
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq COND then ␈↓↓nevcon[␈↓αd ␈↓↓e, a]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse if a ␈↓↓e ␈↓αeq LIST then ␈↓↓mapcar[␈↓αd ␈↓↓e , λx: neval[x, a]]
␈↓ ↓H␈↓↓␈↓ α_ ␈↓αelse ␈↓↓neval[␈↓αd ␈↓↓assoc[␈↓αa ␈↓↓e, a] . ␈↓αd ␈↓↓e, a]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓e ␈↓αeq LAMBDA then ␈↓↓neval[␈↓αadda ␈↓↓e, nprup[␈↓αada ␈↓↓e, ␈↓αd ␈↓↓e, a]]
␈↓ ↓H␈↓↓␈↓ α_␈↓αelse if aa ␈↓↓e ␈↓αeq LABEL then ␈↓↓neval[␈↓αadda ␈↓↓e . ␈↓αd ␈↓↓e, [␈↓αada ␈↓↓e . ␈↓αa ␈↓↓e] . a], ␈↓
␈↓ ↓H␈↓↓␈↓ α_where the auxiliary function nevcon␈↓ is given by
␈↓ ↓H␈↓␈↓ α_␈↓↓nevcon[u, a] ← ␈↓αif ␈↓↓neval[␈↓αaa ␈↓↓u, a] ␈↓αthen ␈↓↓neval[␈↓αada ␈↓↓u, a] ␈↓αelse ␈↓↓nevcon[␈↓αd ␈↓↓u, a].␈↓
␈↓ ↓H␈↓␈↓ α_and nprup is
␈↓ ↓H␈↓␈↓ α_␈↓↓nprup[u, v,a] ← ␈↓αif n ␈↓↓u ␈↓αthen ␈↓↓a ␈↓αelse [a ␈↓↓u . [␈↓αa ␈↓↓v . a]] . nprup[␈↓αd ␈↓↓u, ␈↓αd ␈↓↓v,a].␈↓
␈↓ ↓H␈↓␈↓ α_The␈α
difference␈α
between␈α∞␈↓↓eval␈↓␈α
and␈α
␈↓↓neval␈↓␈α
is␈α∞only␈α
in␈α
two␈α
terms.␈α∞ ␈↓↓eval␈↓␈α
evaluates␈α
a␈α∞variable␈α
by
␈↓ ↓H␈↓looking␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αwhereas␈α
␈↓↓neval␈↓␈αlooks␈αit␈αup␈αon␈αthe␈αassociation␈αlist␈αand␈α
evaluates
␈↓ ↓H␈↓the␈αresult␈αin␈αthe␈αcontext␈αin␈αwhich␈αit␈αwas␈αput␈αon␈αthe␈αassociation␈αlist.␈α Correspondingly,␈αwhen␈αa␈αλ-
␈↓ ↓H␈↓expression␈α
is␈αencountered,␈α
␈↓↓eval␈↓␈α
forms␈αa␈α
new␈α
association␈αlist␈α
by␈α
pairing␈αthe␈α
values␈α
of␈αthe␈α
arguments
␈↓ ↓H␈↓with␈αthe␈α
variables␈αbound␈αby␈α
the␈αλ␈αand␈α
putting␈αthe␈αnew␈α
pairs␈αin␈αfront␈α
of␈αthe␈αold␈α
association␈αlist,
␈↓ ↓H␈↓whereas␈α
␈↓↓neval␈↓␈α
pairs␈α
the␈α
arguments␈α
themselves␈α
with␈αthe␈α
variables␈α
and␈α
puts␈α
them␈α
on␈α
the␈α
front␈αof
␈↓ ↓H␈↓the␈αassociation␈α
list.␈α The␈α
function␈αneval␈α
also␈αsaves␈αthe␈α
current␈αassociation␈α
list␈αwith␈α
each␈αvariable
␈↓ ↓H␈↓on␈αthe␈αassociation␈αlist,␈αso␈αthat␈αthe␈αvariables␈αcan␈αbe␈αevaluated␈αin␈αthe␈αcorrect␈αcontext.␈α In␈αmost␈αcases
␈↓ ↓H␈↓both␈α
give␈αthe␈α
same␈αresult␈α
with␈αthe␈α
same␈αwork,␈α
but␈α␈↓↓neval␈↓␈α
gives␈αa␈α
result␈αin␈α
some␈αcases␈α
in␈αwhich␈α
␈↓↓eval␈↓
␈↓ ↓H␈↓loops. An example is obtained by evaluating ␈↓↓F[2, 1]␈↓ where ␈↓↓F␈↓ is defined by
␈↓ ↓H␈↓␈↓ α_␈↓↓F[x, y] ← ␈↓αif␈↓↓ x=0 ␈↓αthen 0 else ␈↓↓F[x-1, F[y-2, x]].␈↓
␈↓ ↓H␈↓Exercises
␈↓ ↓H␈↓␈↓ ¬cCHAPTER IV␈↓ *68
␈↓ ↓H␈↓␈↓ α_1.␈αWrite␈α␈↓↓neval␈↓␈αand␈αthe␈αnecessary␈αauxiliary␈αfunctions␈αin␈αlist␈αform,␈αand␈αtry␈αthem␈αout␈αon␈αsome
␈↓ ↓H␈↓examples.
␈↓ ↓H␈↓2. ␈↓αComputability.␈↓
␈↓ ↓H␈↓␈↓ α_Some␈α∞LISP␈α
calculations␈α∞run␈α∞on␈α
indefinitely.␈α∞ The␈α∞most␈α
trivial␈α∞case␈α∞occurs␈α
if␈α∞we␈α∞make␈α
the
␈↓ ↓H␈↓recursive definition
␈↓ ↓H␈↓␈↓ α_␈↓↓loop x ← loop x␈↓
␈↓ ↓H␈↓and␈α
attempt␈α
to␈α
compute␈α
␈↓↓loop[x]␈↓␈α
for␈α
any␈α
␈↓↓x␈↓␈α
whatsoever.␈α
Don't␈α
dismiss␈α
this␈α
example␈α
just␈αbecause␈α
no-
␈↓ ↓H␈↓one␈αwould␈αwrite␈αsuch␈αan␈αobviously␈αuseless␈αfunction␈αdefinition.␈α There␈αis␈αa␈αsense␈αin␈αwhich␈αit␈αis␈α
the
␈↓ ↓H␈↓"zero"␈α
of␈α∞a␈α
large␈α∞class␈α
of␈α∞non¬terminating␈α
function␈α∞definitions,␈α
and,␈α∞as␈α
the␈α∞Romans␈α
experienced
␈↓ ↓H␈↓but never learned, leaving zero out of the number system is a mistake.
␈↓ ↓H␈↓␈↓ α_Nevertheless,␈αin␈αmost␈α
programming,␈αnon-terminating␈αcalculations␈αare␈α
the␈αresults␈αof␈αerrors␈α
in
␈↓ ↓H␈↓defining␈αfunctions.␈α Therefore,␈αit␈αwould␈αbe␈αuseful␈αto␈αbe␈αable␈αto␈αtell␈αwhether␈αa␈αfunction␈αdefinition
␈↓ ↓H␈↓gives␈αa␈αresult␈α
for␈αall␈αarguments.␈α In␈α
fact,␈αit␈αwould␈αbe␈α
useful␈αto␈αbe␈αable␈α
to␈αtell␈αwhether␈α
a␈αfunction
␈↓ ↓H␈↓will terminate for a single argument. Let us make this goal more precise.
␈↓ ↓H␈↓␈↓ α_Suppose␈α∞that␈α
␈↓↓f␈↓␈α∞is␈α∞a␈α
LISP␈α∞function␈α∞and␈α
␈↓↓a␈↓␈α∞is␈α∞an␈α
S-expression,␈α∞and␈α∞we␈α
would␈α∞like␈α∞to␈α
know
␈↓ ↓H␈↓whether␈α
the␈α
computation␈αof␈α
␈↓↓f[a]␈↓␈α
terminates.␈α
Suppose␈α␈↓↓f␈↓␈α
is␈α
represented␈αby␈α
the␈α
S-expression␈α
␈↓↓f*␈↓␈αin
␈↓ ↓H␈↓the␈α⊂usual␈α∂S-expression␈α⊂notation␈α∂for␈α⊂LISP␈α∂functions.␈α⊂ Then␈α∂the␈α⊂S-expression␈α∂(f*␈α⊂(QUOTE␈α∂a))
␈↓ ↓H␈↓represents␈α␈↓↓f[a]␈↓.␈α Define␈αthe␈αfunction␈α␈↓↓term␈↓␈αby␈αgiving␈α␈↓↓term[e]␈↓␈αthe␈αvalue␈α␈↓αtrue␈↓↓␈αif␈αe␈↓␈αis␈αan␈αS-expression
␈↓ ↓H␈↓of␈αthe␈α
form␈α(f*␈α
(QUOTE␈αa))␈α
for␈αwhich␈α
␈↓↓f[a]␈↓␈αterminates␈α
and␈α␈↓αfalse␈↓␈α
otherwise.␈α We␈α
now␈αask␈α
whether
␈↓ ↓H␈↓␈↓↓term␈↓␈α
is␈α∞a␈α
LISP␈α∞function,␈α
i.e.␈α
can␈α∞it␈α
be␈α∞constructed␈α
from␈α∞␈↓↓car,␈α
cdr,␈α
cons,␈α∞atom,␈α
␈↓␈α∞and␈α
␈↓↓eq␈↓␈α∞using␈α
λ,
␈↓ ↓H␈↓␈↓αlabel␈↓,␈αand␈αconditional␈α
expressions?␈α Well,␈αit␈α
can't,␈αas␈αwe␈α
shall␈αshortly␈αprove,␈α
and␈αthis␈αmeans␈αthat␈α
it
␈↓ ↓H␈↓is␈α∞not␈α∞␈↓↓computable␈↓␈α∞whether␈α∞a␈α∞LISP␈α∂calculation␈α∞terminates,␈α∞since␈α∞if␈α∞␈↓↓term␈↓␈α∞were␈α∞computable␈α∂by␈α∞any
␈↓ ↓H␈↓computer␈α∂or␈α∂in␈α∂any␈α∞recognized␈α∂sense,␈α∂it␈α∂could␈α∞be␈α∂represented␈α∂as␈α∂a␈α∞LISP␈α∂function.␈α∂ Here␈α∂is␈α∞the
␈↓ ↓H␈↓proof:
␈↓ ↓H␈↓␈↓ α_Consider the function ␈↓↓terma␈↓ defined from ␈↓↓term␈↓ by
␈↓ ↓H␈↓␈↓ α_␈↓↓terma u ← ␈↓αif␈↓↓ term list[u, list[QUOTE, u]] ␈↓αthen␈↓↓ loop u ␈↓αelse true␈↓,
␈↓ ↓H␈↓and␈α∞suppose␈α∞that␈α∞␈↓↓f␈↓␈α∞is␈α∞a␈α∞LISP␈α∞function␈α
and␈α∞that␈α∞␈↓↓f*␈↓␈α∞is␈α∞its␈α∞S-expression␈α∞representation.␈α∞ What␈α
is
␈↓ ↓H␈↓␈↓↓terma␈αf*␈↓?␈α Well␈α␈↓↓terma␈αf*␈↓␈αtells␈αus␈αwhether␈αthe␈αcomputation␈αof␈α␈↓↓f[f*]␈↓␈αterminates,␈αand␈αit␈αtells␈αus␈αthis
␈↓ ↓H␈↓by␈α∞going␈α∂into␈α∞a␈α∂loop␈α∞if␈α∂␈↓↓f[f*]␈↓␈α∞terminates␈α∂and␈α∞giving␈α∂␈↓αtrue␈↓␈α∞otherwise.␈α∂ Now␈α∞if␈α∂␈↓↓term␈↓␈α∞were␈α∂a␈α∞LISP
␈↓ ↓H␈↓function,␈α
then␈α
␈↓↓terma␈↓␈α
would␈αalso␈α
be␈α
a␈α
LISP␈αfunction.␈α
Indeed␈α
if␈α
␈↓↓term␈↓␈αwere␈α
represented␈α
by␈α
the␈αS-
␈↓ ↓H␈↓expression ␈↓↓term*␈↓, then ␈↓↓terma␈↓ would be represented by the S-expression
␈↓ ↓H␈↓␈↓ α_␈↓↓terma*␈α=␈α(LAMBDA␈α
(U)␈α(COND␈α((term*␈α
(LIST␈αU␈α(LIST␈α
(QUOTE␈αQUOTE)␈αU)))␈α(LOOP␈α
U))
␈↓ ↓H␈↓↓(T T))).
␈↓ ↓H␈↓Now␈α∩consider␈α∩␈↓↓terma[terma*]␈↓.␈α⊃ According␈α∩to␈α∩the␈α⊃definition␈α∩of␈α∩␈↓↓terma␈↓,␈α⊃this␈α∩will␈α∩tell␈α∩us␈α⊃whether
␈↓ ↓H␈↓␈↓↓terma[terma*]␈↓␈αis␈αdefined,␈αi.e.␈αit␈αtells␈αabout␈αitself.␈α However,␈αit␈αgives␈αthis␈αanswer␈αin␈αa␈αcontradictory
␈↓ ↓H␈↓␈↓ ¬cCHAPTER IV␈↓ *69
␈↓ ↓H␈↓way;␈α
namely␈α␈↓↓terma[terma*]␈↓␈α
looping␈α
tells␈αus␈α
that␈α
␈↓↓terma[terma*]␈↓␈αterminates,␈α
and␈α␈↓↓terma[terma*]␈↓␈α
being
␈↓ ↓H␈↓␈↓αtrue␈↓␈α∞tells␈α∞us␈α
that␈α∞␈↓↓terma[terma*]␈↓␈α∞doesn't␈α∞terminate.␈α
This␈α∞contradiction␈α∞tells␈α∞us␈α
that␈α∞␈↓↓term␈↓␈α∞is␈α∞not␈α
a
␈↓ ↓H␈↓LISP␈α∃function,␈α∃and␈α∃there␈α∃is␈α∃no␈α∀general␈α∃procedure␈α∃for␈α∃telling␈α∃whether␈α∃a␈α∃LISP␈α∀calculation
␈↓ ↓H␈↓terminates.
␈↓ ↓H␈↓␈↓ α_The␈α∩above␈α⊃result␈α∩does␈α⊃not␈α∩exclude␈α⊃LISP␈α∩functions␈α⊃that␈α∩tell␈α⊃whether␈α∩LISP␈α⊃calculations
␈↓ ↓H␈↓terminate.␈α⊃ It␈α⊃just␈α⊃excludes␈α∩perfect␈α⊃ones.␈α⊃ Suppose␈α⊃we␈α⊃have␈α∩a␈α⊃function␈α⊃␈↓↓t␈↓␈α⊃that␈α∩sometimes␈α⊃says
␈↓ ↓H␈↓calculations␈α∂terminate,␈α∞sometimes␈α∂says␈α∂they␈α∞don't␈α∂terminate,␈α∂and␈α∞sometimes␈α∂runs␈α∂on␈α∞indefinitely.
␈↓ ↓H␈↓We␈α
shall␈α
further␈α
assume␈α
that␈α
when␈α
␈↓↓t␈↓␈α
gives␈αan␈α
answer␈α
it␈α
is␈α
always␈α
right.␈α
Given␈α
such␈α
a␈αfunction␈α
we
␈↓ ↓H␈↓can␈αimprove␈αit␈α
a␈αbit␈αso␈α
that␈αit␈αwill␈α
always␈αgive␈αthe␈α
right␈αanswer␈αwhen␈α
the␈αcalculation␈αit␈α
is␈αasked
␈↓ ↓H␈↓about␈αterminates.␈α This␈αis␈αdone␈αby␈αmixing␈αthe␈αcomputation␈αof␈α␈↓↓t[e]␈↓␈αwith␈αa␈αcomputation␈α
of␈α␈↓↓eval[e,
␈↓ ↓H␈↓↓NIL]␈↓␈α
doing␈αthe␈α
computations␈αalternately.␈α
If␈αthe␈α
␈↓↓eval[e,␈αNIL]␈↓␈α
computation␈αever␈α
terminates,␈αthen
␈↓ ↓H␈↓the new function asserts termination.
␈↓ ↓H␈↓␈↓ α_Given␈α
such␈α
a␈α
␈↓↓t␈↓,␈α
we␈α
can␈α
always␈α
find␈α
a␈α
calculation␈α
that␈α
does␈α
not␈α
terminate␈α
but␈α
␈↓↓t␈↓␈α
doesn't␈αsay␈α
so.
␈↓ ↓H␈↓The construction is just like that used in the previous proof. Given ␈↓↓t␈↓, we construct
␈↓ ↓H␈↓␈↓ α_␈↓↓ta u ← ␈↓αif␈↓↓ t list[u, list[QUOTE, u]] ␈↓αthen␈↓↓ loop u ␈↓αelse true␈↓,
␈↓ ↓H␈↓and␈α
then␈αwe␈α
consider␈α␈↓↓ta[ta*]␈↓.␈α
If␈α
this␈αhad␈α
the␈αvalue␈α
␈↓αtrue␈↓,␈α
then␈αit␈α
wouldn't␈αterminate␈α
so␈αtherefore␈α
it
␈↓ ↓H␈↓doesn't␈α⊃terminate␈α⊃but␈α⊂is␈α⊃not␈α⊃one␈α⊂of␈α⊃those␈α⊃expressions␈α⊂which␈α⊃␈↓↓t␈↓␈α⊃decides.␈α⊂ Thus␈α⊃for␈α⊃any␈α⊂partial
␈↓ ↓H␈↓decider␈α∂we␈α∂can␈α∞find␈α∂a␈α∂LISP␈α∂calculation␈α∞which␈α∂doesn't␈α∂terminate␈α∞but␈α∂which␈α∂the␈α∂decider␈α∞doesn't
␈↓ ↓H␈↓decide.
␈↓ ↓H␈↓␈↓ α_This can in turn be used to get a slightly better decider, namely
␈↓ ↓H␈↓␈↓ α_␈↓↓t␈↓∧1[␈↓↓e] ← ␈↓αif␈↓↓ e = ta* ␈↓αthen DOESN'T-TERMINATE else␈↓↓ t[e]␈↓.
␈↓ ↓H␈↓Of␈αcourse,␈α␈↓↓t␈↓∧1␈↓␈αisn't␈αmuch␈αbetter␈αthan␈α␈↓↓t␈↓,␈αsince␈αit␈αcan␈αdecide␈αonly␈αone␈αmore␈αcomputation,␈αbut␈αwe␈αcan
␈↓ ↓H␈↓form␈α␈↓↓t␈↓∧2␈↓␈αby␈αapplying␈αthe␈αsame␈αprocess,␈αand␈α
so␈αforth.␈α In␈αfact,␈αwe␈αcan␈αeven␈αform␈α␈↓↓t␈↓∧∞␈↓␈α
which␈αdecides
␈↓ ↓H␈↓all␈αthe␈αcases␈αdecided␈αby␈αany␈α␈↓↓t␈↓∧n␈↓.␈α This␈αcan␈αbe␈αfurther␈αimproved␈αby␈αthe␈αsame␈αprocess,␈αetc.␈α How␈αfar
␈↓ ↓H␈↓can␈α∂we␈α∂go?␈α∞ The␈α∂answer␈α∂is␈α∞technical;␈α∂namely,␈α∂the␈α∞improvement␈α∂process␈α∂can␈α∞be␈α∂carried␈α∂out␈α∞any
␈↓ ↓H␈↓recursive ordinal number of times.
␈↓ ↓H␈↓␈↓ α_Unfortunately,␈α⊂this␈α⊂kind␈α∂of␈α⊂improvement␈α⊂seems␈α∂to␈α⊂be␈α⊂superficial,␈α∂since␈α⊂none␈α⊂of␈α⊂the␈α∂new
␈↓ ↓H␈↓computations proved not to terminate are likely to be of practical interest.
␈↓ ↓H␈↓Exercises.
␈↓ ↓H␈↓1. Write a function that gives ␈↓↓t␈↓∧n+1␈↓ in terms of ␈↓↓t␈↓∧n␈↓.
␈↓ ↓H␈↓2. Write a function that gives ␈↓↓t␈↓∧∞␈↓ in terms of ␈↓↓t␈↓.
␈↓ ↓H␈↓3.␈α∞If␈α∞you␈α∞know␈α∞about␈α
Turing␈α∞machines,␈α∞write␈α∞a␈α∞LISP␈α
function␈α∞to␈α∞simulate␈α∞an␈α∞arbitrary␈α
Turing
␈↓ ↓H␈↓machine given a description of the machine in some convenient notation.
␈↓ ↓H␈↓4.␈α
Write␈α
a␈α
LISP␈α
function␈αthat␈α
will␈α
translate␈α
a␈α
Turing␈αmachine␈α
description␈α
into␈α
a␈α
LISP␈αfunction
␈↓ ↓H␈↓that will do the same computation.
␈↓ ↓H␈↓␈↓ ¬cCHAPTER IV␈↓ *70
␈↓ ↓H␈↓5.␈αIf␈αyou␈αreally␈αlike␈αTuring␈αmachines,␈αwrite␈αa␈αdescription␈αof␈αa␈αTuring␈αmachine␈αthat␈αwill␈αinterpret
␈↓ ↓H␈↓LISP internal notation.
␈↓ ↓H␈↓␈↓ εH␈↓ *71
␈↓ ↓H␈↓α␈↓ εαChapter V
␈↓ ↓H␈↓α␈↓ ∧(PROVING LISP PROGRAMS CORRECT
␈↓ ↓H␈↓␈↓ α_In␈α⊃this␈α∩chapter␈α⊃we␈α⊃will␈α∩introduce␈α⊃techniques␈α⊃for␈α∩proving␈α⊃LISP␈α⊃programs␈α∩correct.␈α⊃ The
␈↓ ↓H␈↓techniques␈αwill␈αmainly␈αbe␈αlimited␈αto␈αwhat␈αwe␈αmay␈αcall␈α␈↓↓clean␈↓␈αLISP␈αprograms.␈α In␈α
particular,␈αthere
␈↓ ↓H␈↓must␈α
be␈αno␈α
side␈α
effects,␈αbecause␈α
our␈αmethods␈α
depend␈α
on␈αthe␈α
ability␈αto␈α
replace␈α
subexpressions␈αby
␈↓ ↓H␈↓equal expressions.
␈↓ ↓H␈↓␈↓ α_The␈α necessary␈αbasic␈αfacts␈αcan␈αbe␈αdivided␈αinto␈αseveral␈αcategories:␈αfirst␈αorder␈αlogic␈αincluding
␈↓ ↓H␈↓conditional␈α↔forms␈α_and␈α↔first␈α↔order␈α_lambda-expressions,␈α↔algebraic␈α↔facts␈α_about␈α↔lists␈α_and␈α↔S-
␈↓ ↓H␈↓expressions,␈αfacts␈αabout␈αthe␈αinductive␈αstructure␈αof␈αlists␈αand␈αS-expressions,␈αand␈αgeneral␈αfacts␈αabout
␈↓ ↓H␈↓functions defined by recursion.
␈↓ ↓H␈↓1. ␈↓αFirst order logic with conditional forms and lambda-expressions.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∂logic␈α∂we␈α∂shall␈α∂use␈α∂is␈α∂called␈α∂first␈α∂order␈α∂logic␈α∂with␈α∂equality,␈α∂but␈α∂we␈α∂will␈α∂extend␈α⊂it␈α∂by
␈↓ ↓H␈↓allowing␈αconditional␈αforms␈αto␈αbe␈αterms␈αand␈αlambda-expressions␈αto␈αbe␈αfunction␈αexpressions.␈α From
␈↓ ↓H␈↓the␈α∞mathematical␈α∞point␈α∂of␈α∞view,␈α∞these␈α∂extensions␈α∞are␈α∞inessential,␈α∞because,␈α∂as␈α∞we␈α∞shall␈α∂see,␈α∞every
␈↓ ↓H␈↓sentence␈αthat␈α
includes␈αconditional␈αforms␈α
or␈αfirst␈αorder␈α
lambdas␈αcan␈αreadily␈α
be␈αtransformed␈αinto␈α
an
␈↓ ↓H␈↓equivalent␈α
sentence␈α
without␈α
them.␈α
However,␈αthe␈α
extensions␈α
are␈α
practically␈α
important,␈αbecause␈α
they
␈↓ ↓H␈↓permit us to use recursive definitions directly as formulas of the logic.
␈↓ ↓H␈↓␈↓ α_Formulas␈α∂of␈α∞the␈α∂logic␈α∞are␈α∂built␈α∂from␈α∞constants,␈α∂variables␈α∞predicate␈α∂symbols,␈α∂and␈α∞function
␈↓ ↓H␈↓symbols␈α⊂using␈α⊂function␈α∂application,␈α⊂conditional␈α⊂forms,␈α∂boolean␈α⊂forms,␈α⊂lambda␈α⊂expressions,␈α∂and
␈↓ ↓H␈↓quantifiers.
␈↓ ↓H␈↓␈↓αConstants␈↓:␈α∞We␈α
will␈α∞use␈α∞S-expresssions␈α
as␈α∞constants␈α
standing␈α∞for␈α∞themselves␈α
and␈α∞also␈α∞lower␈α
case
␈↓ ↓H␈↓letters␈α∩from␈α∩the␈α∩first␈α∩part␈α∩of␈α∩the␈α∪alphabet␈α∩to␈α∩represent␈α∩constants␈α∩in␈α∩other␈α∩domains␈α∪than␈α∩S-
␈↓ ↓H␈↓expressions.
␈↓ ↓H␈↓␈↓αVariables␈↓:␈αWe␈αwill␈αuse␈αthe␈αletters␈α␈↓↓u␈↓␈αthru␈α␈↓↓z␈↓␈αwith␈αor␈αwithout␈αsubscripts␈αas␈αvariables.␈α The␈αvariables
␈↓ ↓H␈↓␈↓↓u␈↓ and ␈↓↓v␈↓ will usually stand for lists while ␈↓↓x␈↓ thru ␈↓↓z␈↓ will stand for S-expressions.
␈↓ ↓H␈↓␈↓αFunction␈αsymbols␈↓:␈αThe␈α
letters␈α␈↓↓f␈↓,␈α␈↓↓g␈↓␈α
and␈α␈↓↓h␈↓␈αwith␈α
or␈αwithout␈αsubscripts␈α
are␈αused␈αas␈αfunction␈α
symbols.
␈↓ ↓H␈↓The␈α⊂LISP␈α⊃function␈α⊂symbols␈α⊃␈↓αa␈↓,␈α⊂␈↓αd␈↓␈α⊃and␈α⊂.␈α⊃(as␈α⊂an␈α⊃infix)␈α⊂are␈α⊃also␈α⊂used␈α⊃as␈α⊂function␈α⊃symbols.␈α⊂ We
␈↓ ↓H␈↓suppose␈α
that␈αeach␈α
function␈αsymbol␈α
takes␈αthe␈α
same␈αdefinite␈α
number␈αof␈α
arguments␈αevery␈α
time␈α
it␈αis
␈↓ ↓H␈↓used.␈α We␈αwill␈αoften␈αuse␈αone␈αargument␈α
functions␈αsymbols␈αas␈αprefixes,␈αi.e.␈α without␈αbrackets,␈αjust␈α
as
␈↓ ↓H␈↓␈↓αa␈↓ and ␈↓αd␈↓ have been used up to now.
␈↓ ↓H␈↓␈↓αPredicate␈α∩symbols␈↓:␈α∩The␈α⊃letters␈α∩␈↓↓p␈↓,␈α∩␈↓↓q␈↓␈α⊃and␈α∩␈↓↓r␈↓␈α∩with␈α⊃or␈α∩without␈α∩subscripts␈α⊃are␈α∩used␈α∩as␈α⊃predicate
␈↓ ↓H␈↓symbols.␈α∂ We␈α∂will␈α∂also␈α∂use␈α∞the␈α∂LISP␈α∂predicate␈α∂symbol␈α∂␈↓αat␈↓␈α∞as␈α∂a␈α∂constant␈α∂predicate␈α∂symbol.␈α∞ The
␈↓ ↓H␈↓equality␈αsymbol␈α=␈α
is␈αalso␈αused␈α
as␈αan␈αinfix.␈α We␈α
suppose␈αthat␈αeach␈α
predicate␈αsymbol␈αtakes␈αthe␈α
same
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *72
␈↓ ↓H␈↓definite␈α∞number␈α
of␈α∞arguments␈α
each␈α∞time␈α
it␈α∞is␈α
used.␈α∞ Infix␈α
and␈α∞prefix␈α
notation␈α∞will␈α
also␈α∞be␈α
used
␈↓ ↓H␈↓where this is customary.
␈↓ ↓H␈↓␈↓ α_Next␈αwe␈αdefine␈αterms,␈αsentences,␈αfunction␈αexpressions␈αand␈αpredicate␈αexpressions␈αinductively.
␈↓ ↓H␈↓A␈αterm␈αis␈αan␈αexpression␈αwhose␈αvalue␈αwill␈αbe␈αan␈αobject␈αlike␈αan␈αS-expression␈αor␈αa␈αnumber␈αwhile␈αa
␈↓ ↓H␈↓sentence␈αhas␈αvalue␈αT␈αor␈αF.␈α Terms␈αare␈αused␈αin␈αmaking␈αsentences,␈αand␈αsentences␈αoccur␈αin␈αterms␈αso
␈↓ ↓H␈↓that␈α∪the␈α∪definitions␈α∪are␈α∪␈↓↓mutually␈α∪recursive␈↓␈α∪where␈α∪this␈α∪use␈α∪of␈α∪the␈α∪word␈α∪␈↓↓recursive␈↓␈α∪should␈α∪be
␈↓ ↓H␈↓distinguished␈α⊂from␈α⊂its␈α∂use␈α⊂in␈α⊂recursive␈α∂definitions␈α⊂of␈α⊂functions.␈α∂ Function␈α⊂expressions␈α⊂are␈α∂also
␈↓ ↓H␈↓involved in the mutual recursion.
␈↓ ↓H␈↓␈↓αTerms␈↓:␈α⊂Constants␈α⊃are␈α⊂terms,␈α⊃and␈α⊂variables␈α⊃are␈α⊂terms.␈α⊃ If␈α⊂␈↓↓f␈↓␈α⊃is␈α⊂a␈α⊃function␈α⊂expression␈α⊃taking␈α⊂␈↓↓n␈↓
␈↓ ↓H␈↓arguments,␈α
and␈α
␈↓↓t␈↓∧1␈↓↓, ... ,t␈↓∧n␈↓↓␈↓␈α
are␈α
terms,␈α
then␈α
␈↓↓f[t␈↓∧1␈↓↓, ... ,t␈↓∧n␈↓↓]␈↓␈α
is␈α
a␈α
term.␈α
If␈α
␈↓↓p␈↓␈α
is␈α
a␈α
sentence␈α
and␈α
␈↓↓t␈↓∧1␈↓↓␈↓␈αand␈α
␈↓↓t␈↓∧2␈↓
␈↓ ↓H␈↓are␈α
terms,␈α∞then␈α
␈↓αif␈↓↓ p ␈↓αthen␈↓↓ t␈↓∧1␈↓↓ ␈↓αelse␈↓↓ t␈↓∧2␈↓␈α
is␈α∞a␈α
term.␈α
We␈α∞soften␈α
the␈α
notation␈α∞by␈α
allowing␈α∞infix␈α
symbols
␈↓ ↓H␈↓where this is customary.
␈↓ ↓H␈↓␈↓αSentences␈↓:␈α⊂If␈α⊃␈↓↓p␈↓␈α⊂is␈α⊂a␈α⊃predicate␈α⊂expression␈α⊂taking␈α⊃␈↓↓n␈↓␈α⊂arguments␈α⊂and␈α⊃␈↓↓t␈↓∧1␈↓↓, ... ,t␈↓∧n␈↓↓␈↓␈α⊂are␈α⊃terms,␈α⊂then
␈↓ ↓H␈↓␈↓↓p[t␈↓∧1␈↓↓, ... ,t␈↓∧n␈↓↓]␈↓␈α∪is␈α∪a␈α∀sentence.␈α∪ Equality␈α∪is␈α∀also␈α∪used␈α∪as␈α∪an␈α∀infixed␈α∪predicate␈α∪symbol␈α∀to␈α∪form
␈↓ ↓H␈↓sentences,␈αi.e.␈α␈↓↓t␈↓∧1␈↓↓ = t␈↓∧2␈↓␈αis␈αa␈αsentence.␈α
If␈α␈↓↓p␈↓␈αis␈αa␈αsentence,␈αthen␈α␈↓↓¬p␈↓␈α
is␈αalso␈αa␈αsentence.␈α If␈α␈↓↓p␈↓␈αand␈α
␈↓↓q␈↓␈αare
␈↓ ↓H␈↓sentences,␈α∃then␈α∃␈↓↓p∧q␈↓,␈α∃␈↓↓p∨q␈↓,␈α∃␈↓↓p⊃q␈↓,␈α∃and␈α∃␈↓↓p≡q␈↓␈α∃are␈α∀sentences.␈α∃ If␈α∃␈↓↓p␈↓,␈α∃␈↓↓q␈↓␈α∃and␈α∃␈↓↓r␈↓␈α∃are␈α∃sentences,␈α∀then
␈↓ ↓H␈↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ r␈↓␈α
is␈α
a␈α
sentence.␈α
If␈α
␈↓↓x␈↓∧1␈↓↓, ..., x␈↓∧n␈↓↓␈↓␈α
are␈αvariables,␈α
and␈α
␈↓↓p␈↓␈α
is␈α
a␈α
term,␈α
then␈α␈↓↓∀x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:t␈↓␈α
and
␈↓ ↓H␈↓␈↓↓∀x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:t␈↓ are sentences.
␈↓ ↓H␈↓␈↓αFunction␈α
expressions␈↓:␈α∞A␈α
function␈α
symbol␈α∞is␈α
a␈α
function␈α∞expression.␈α
If␈α
␈↓↓x␈↓∧1␈↓↓, ... ,x␈↓∧n␈↓↓␈↓␈α∞are␈α
variables
␈↓ ↓H␈↓and ␈↓↓t␈↓ is a term, then ␈↓↓[λx␈↓∧1␈↓↓, ... ,x␈↓∧n␈↓↓:t]␈↓ is a function expression.
␈↓ ↓H␈↓␈↓αPredicate␈αexpressions␈↓:␈αA␈αpredicate␈αsymbol␈αis␈αa␈αpredicate␈αexpression.␈α If␈α␈↓↓x␈↓∧1␈↓↓, ... ,x␈↓∧n␈↓↓␈↓␈αare␈αvariables
␈↓ ↓H␈↓and ␈↓↓p␈↓ is a sentence, then ␈↓↓[λx␈↓∧1␈↓↓, ... ,x␈↓∧n␈↓↓:p]␈↓ is a predicate expression.
␈↓ ↓H␈↓␈↓ α_An␈αoccurrence␈αof␈αa␈αvariable␈α␈↓↓x␈↓␈αis␈αcalled␈αbound␈αif␈αit␈αis␈αin␈αan␈αexpression␈αof␈αone␈αof␈αthe␈αforms
␈↓ ↓H␈↓␈↓↓[λx␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:t]␈↓,␈α
␈↓↓[λx␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:p]␈↓,␈α
␈↓↓[∀x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:p]␈↓␈αor␈α
␈↓↓[∃x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:p]␈↓␈α
where␈α␈↓↓x␈↓␈α
is␈α
one␈αof␈α
the␈α
numbered␈α␈↓↓x␈↓'s.␈α
If
␈↓ ↓H␈↓not bound an occurrence is called free.
␈↓ ↓H␈↓␈↓ α_The␈α
␈↓↓semantics␈↓␈αof␈α
first␈αorder␈α
logic␈αconsists␈α
of␈α
the␈αrules␈α
that␈αenable␈α
us␈αto␈α
determine␈α
when␈αa
␈↓ ↓H␈↓sentence␈αis␈αtrue␈αand␈αwhen␈αit␈αis␈αfalse.␈α However,␈αthe␈αtruth␈αor␈αfalsity␈αof␈αa␈αsentence␈αis␈αrelative␈αto␈αthe
␈↓ ↓H␈↓interpretation␈αassigned␈αto␈αthe␈αconstants,␈αthe␈αfunction␈αand␈αpredicate␈αsymbols␈αand␈αthe␈αfree␈α
variables
␈↓ ↓H␈↓of the formula. We proceed as follows:
␈↓ ↓H␈↓␈↓ α_We␈α
begin␈αby␈α
choosing␈α
a␈αdomain.␈α
In␈αmost␈α
cases␈α
we␈αshall␈α
consider␈αthe␈α
domain␈α
will␈αinclude
␈↓ ↓H␈↓the␈αS-expressions␈αand␈αany␈αS-expression␈αconstants␈αappearing␈αin␈αthe␈αformula␈αstand␈αfor␈αthemselves.
␈↓ ↓H␈↓We␈α
will␈α
allow␈α
for␈α
the␈α
possibility␈α
that␈α
other␈α
objects␈α
than␈α
S-expressions␈α
exist,␈α
and␈α
some␈αconstants
␈↓ ↓H␈↓may␈α
designate␈αthem.␈α
Each␈αfunction␈α
or␈αpredicate␈α
symbol␈αis␈α
assigned␈αa␈α
function␈αor␈α
predicate␈αon␈α
the
␈↓ ↓H␈↓domain.␈α∃ We␈α∃will␈α⊗normally␈α∃assign␈α∃to␈α⊗the␈α∃basic␈α∃LISP␈α⊗function␈α∃and␈α∃predicate␈α⊗symbols␈α∃the
␈↓ ↓H␈↓corresponding␈αbasic␈αLISP␈α
functions␈αand␈αpredicates.␈α
Each␈αvariable␈αappearing␈α
free␈αin␈αa␈αsentence␈α
is
␈↓ ↓H␈↓also␈αassigned␈αan␈αelemet␈αof␈αthe␈αdomain.␈α
All␈αthese␈αassignments␈αconstitute␈αan␈αinterpretation,␈αand␈α
the
␈↓ ↓H␈↓truth of a sentence is relative to the interpretation.
␈↓ ↓H␈↓␈↓ α_The␈α⊂truth␈α∂of␈α⊂a␈α∂sentence␈α⊂is␈α⊂determined␈α∂from␈α⊂the␈α∂values␈α⊂of␈α∂its␈α⊂constituents␈α⊂by␈α∂evaluating
␈↓ ↓H␈↓successively␈α∞larger␈α∞subexpressions.␈α
The␈α∞rules␈α∞for␈α
handling␈α∞functions␈α∞and␈α∞predicates,␈α
conditional
␈↓ ↓H␈↓expressions,␈αequality,␈αand␈αBoolean␈αexpressions␈αare␈αexactly␈αthe␈αsame␈αas␈αthose␈αwe␈αhave␈αused␈αin␈αthe
␈↓ ↓H␈↓previous chapters. We need only explain quantifiers:
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *73
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:e␈↓␈αis␈αassigned␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓e␈↓␈αis␈αassigned␈αtrue␈αfor␈αall␈αassignments␈αof␈αelements
␈↓ ↓H␈↓of␈αthe␈αdomain␈αto␈αthe␈α␈↓↓x␈↓'s.␈α If␈α␈↓↓e␈↓␈αhas␈αfree␈αvariables␈αthat␈αare␈αnot␈αamong␈αthe␈α␈↓↓x␈↓'s,␈αthen␈αthe␈αtruth␈αof␈αthe
␈↓ ↓H␈↓sentence␈α
depends␈α
on␈α
the␈αvalues␈α
assigned␈α
to␈α
these␈αremaining␈α
free␈α
variables.␈α
␈↓↓∃x␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:e␈↓␈αis␈α
assigned
␈↓ ↓H␈↓true␈αif␈αand␈αonly␈αif␈α␈↓↓e␈↓␈αis␈αassigned␈αtrue␈αfor␈α␈↓↓some␈↓␈αassignment␈αof␈αvalues␈αin␈αthe␈αdomain␈αto␈αthe␈α␈↓↓x␈↓'s.␈α Free
␈↓ ↓H␈↓variables are handled just as before.
␈↓ ↓H␈↓␈↓ α_␈↓↓λx␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:u␈↓␈α⊃is␈α⊃assigned␈α⊂a␈α⊃function␈α⊃or␈α⊂predicate␈α⊃according␈α⊃to␈α⊂whether␈α⊃␈↓↓u␈↓␈α⊃is␈α⊂a␈α⊃term␈α⊃or␈α⊂a
␈↓ ↓H␈↓sentence.␈α
The␈α
value␈α
of␈α
␈↓↓[λx␈↓∧1␈↓↓ ... x␈↓∧n␈↓↓:u][t␈↓∧1␈↓↓,...,t␈↓∧n␈↓↓]␈α
is␈α
obtained␈α
by␈α
evaluating␈α
the␈α
t␈↓'s␈α
and␈α∞using␈α
these
␈↓ ↓H␈↓values␈αas␈α
values␈αof␈α
the␈α␈↓↓x␈↓'s␈α
in␈αthe␈α
evaluation␈αof␈α␈↓↓u␈↓.␈α
If␈α␈↓↓u␈↓␈α
has␈αfree␈α
variables␈αin␈α
addition␈αto␈α
the␈α␈↓↓x␈↓'s,
␈↓ ↓H␈↓the function assigned will depend on them too.
␈↓ ↓H␈↓␈↓ α_Those␈α
who␈αare␈α
familiar␈αwith␈α
the␈α
lambda␈αcalculus␈α
should␈αnote␈α
that␈α
λ␈αis␈α
being␈αused␈α
here␈αin␈α
a
␈↓ ↓H␈↓very␈α
limited␈α
way.␈α Namely,␈α
the␈α
variables␈αin␈α
a␈α
lambda-expression␈α
take␈αonly␈α
elements␈α
of␈αthe␈α
domain
␈↓ ↓H␈↓as␈α⊂values,␈α⊂whereas␈α∂the␈α⊂essence␈α⊂of␈α∂the␈α⊂lambda␈α⊂calculus␈α∂is␈α⊂that␈α⊂they␈α∂take␈α⊂arbitrary␈α⊂functions␈α∂as
␈↓ ↓H␈↓values. We may call these restricted lambda expressions ␈↓↓first order lambdas␈↓.
␈↓ ↓H␈↓2. ␈↓αConditional forms.␈↓
␈↓ ↓H␈↓␈↓ α_All the properties we shall use of conditional forms follow from the relation
␈↓ ↓H␈↓␈↓ α_␈↓↓[p ⊃ [␈↓αif␈↓↓ p ␈↓αthen ␈↓↓a ␈↓αelse␈↓↓ b] = a] ∧ [¬p ⊃ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓b] = b]␈↓.
␈↓ ↓H␈↓(If␈α
we␈α
weren't␈α
adhering␈α
to␈α
the␈α
requirement␈α
that␈α
all␈α
terms␈α
be␈α
defined␈α
for␈α
all␈α
values␈α
of␈αthe␈α
variables,
␈↓ ↓H␈↓the situation would be more complicated).
␈↓ ↓H␈↓␈↓ α_It is, however, worthwhile to list separately some properties of conditional forms.
␈↓ ↓H␈↓␈↓ α_First we have the obvious
␈↓ ↓H␈↓␈↓ α_␈↓αif T then␈↓↓ a ␈↓αelse␈↓↓ b = a␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓αif F then␈↓↓ a ␈↓αelse␈↓↓ b = b␈↓.
␈↓ ↓H␈↓␈↓ α_Next we have a ␈↓↓distributive law␈↓ for functions applied to conditional forms, namely
␈↓ ↓H␈↓␈↓ α_␈↓↓f[␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ f[a] ␈↓αelse␈↓↓ f[b]␈↓.
␈↓ ↓H␈↓This␈α
applies␈αto␈α
predicates␈αas␈α
well␈αas␈α
functions␈αand␈α
can␈α
also␈αbe␈α
used␈αwhen␈α
one␈αof␈α
the␈αarguments␈α
of
␈↓ ↓H␈↓a␈αfunction␈αof␈αseveral␈αarguments␈αis␈αa␈αconditional␈αform.␈α It␈αalso␈αapplies␈αwhen␈αone␈αof␈αthe␈αterms␈αof␈αa
␈↓ ↓H␈↓conditional form is itself a conditional form.
␈↓ ↓H␈↓Thus
␈↓ ↓H␈↓␈↓ α_␈↓αif␈↓↓ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ r] ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ [␈↓αif␈↓↓ q ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ r ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b]␈↓
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *74
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓αif␈↓↓ p ␈↓αthen␈↓↓ [␈↓αif␈↓↓ q ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b] ␈↓αelse␈↓↓ c = ␈↓αif␈↓↓ q ␈↓αthen␈↓↓ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ c] ␈↓αelse␈↓↓ [␈↓αif␈↓↓ p ␈↓αthen␈↓↓ b ␈↓αelse␈↓↓ c]␈↓.
␈↓ ↓H␈↓␈↓ α_When␈αthe␈αexpressions␈αfollowing␈α␈↓αthen␈↓␈αand␈α␈↓αelse␈↓␈αare␈αsentences,␈αthen␈αthe␈αconditional␈αform␈αcan
␈↓ ↓H␈↓be replaced by a sentence according to
␈↓ ↓H␈↓␈↓ α_␈↓↓[␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ r] ≡ [p ∧ q] ∨ [¬p ∧ r]␈↓.
␈↓ ↓H␈↓These␈α
two␈α
rules␈α
permit␈αeliminating␈α
conditional␈α
forms␈α
from␈αsentences␈α
by␈α
first␈α
using␈αdistributivity
␈↓ ↓H␈↓to␈αmove␈αthe␈αconditionals␈αto␈αthe␈αoutside␈αof␈αany␈αfunctions␈αand␈αthen␈αreplacing␈αthe␈αconditional␈αform
␈↓ ↓H␈↓by a Boolean expression.
␈↓ ↓H␈↓␈↓ α_Note␈α
that␈α
the␈α
elimination␈α
of␈α
conditional␈α
forms␈αmay␈α
increase␈α
the␈α
size␈α
of␈α
a␈α
sentence,␈αbecause␈α
␈↓↓p␈↓
␈↓ ↓H␈↓occurs␈αtwice␈αin␈αthe␈αright␈αhand␈αside␈αof␈αthe␈αabove␈αequivalence.␈α In␈αthe␈αmost␈αunfavorable␈αcase,␈α␈↓↓p␈↓␈αis
␈↓ ↓H␈↓dominates␈α∩the␈α⊃size␈α∩of␈α⊃the␈α∩expression␈α⊃so␈α∩that␈α⊃writing␈α∩it␈α⊃twice␈α∩almost␈α⊃doubles␈α∩the␈α⊃size␈α∩of␈α⊃the
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓␈↓ α_Suppose␈αthat␈α␈↓↓a␈↓␈αand␈α␈↓↓b␈↓␈αin␈α␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓␈αare␈αexpressions␈αthat␈αmay␈αcontain␈αthe␈αsentence␈α␈↓↓p␈↓.
␈↓ ↓H␈↓Occurrences␈α
of␈α
␈↓↓p␈↓␈α
in␈α
␈↓↓a␈↓␈α
can␈αbe␈α
replaced␈α
by␈α
T,␈α
and␈α
occurrences␈α
of␈α␈↓↓p␈↓␈α
in␈α
␈↓↓b␈↓␈α
can␈α
be␈α
replaced␈α
by␈αF.␈α
This
␈↓ ↓H␈↓follows from the fact that ␈↓↓a␈↓ is only evaluated if ␈↓↓p␈↓ is true and ␈↓↓b␈↓ is evaluated only if ␈↓↓p␈↓ is false.
␈↓ ↓H␈↓␈↓ α_This␈α∂leads␈α⊂to␈α∂a␈α⊂strengthened␈α∂form␈α⊂of␈α∂the␈α⊂law␈α∂of␈α⊂replacement␈α∂of␈α⊂equals␈α∂by␈α⊂equals.␈α∂ The
␈↓ ↓H␈↓ordinary␈α
form␈α
of␈α
the␈α
law␈α
says␈α
that␈α
if␈α
we␈α
have␈α
␈↓↓e = e'␈↓,␈α
then␈α
we␈α
can␈α
replace␈α
any␈α
occurrence␈α
of␈α
␈↓↓e␈↓␈αin␈α
an
␈↓ ↓H␈↓expression␈α⊃by␈α⊃an␈α⊃occurrence␈α⊃of␈α⊃␈↓↓e'␈↓.␈α⊃ However,␈α⊃if␈α⊂we␈α⊃want␈α⊃to␈α⊃replace␈α⊃␈↓↓e␈↓␈α⊃by␈α⊃␈↓↓e'␈↓␈α⊃within␈α⊃␈↓↓a␈↓␈α⊂within
␈↓ ↓H␈↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b␈↓,␈α
then␈α∞we␈α
need␈α
only␈α∞prove␈α
␈↓↓p ⊃ e =e'␈↓,␈α
and␈α∞to␈α
make␈α
the␈α∞replacement␈α
within␈α∞␈↓↓b␈↓␈α
we
␈↓ ↓H␈↓need only prove ␈↓↓¬p ⊃ e = e'␈↓.
␈↓ ↓H␈↓␈↓ α_Additional␈α∀facts␈α∪about␈α∀conditional␈α∀forms␈α∪are␈α∀given␈α∀in␈α∪(McCarthy␈α∀1963a)␈α∀including␈α∪a
␈↓ ↓H␈↓discussion␈αof␈αcanonical␈αforms␈αthat␈αparallels␈α
the␈αcanonical␈αforms␈αof␈αBoolean␈αforms.␈α
Any␈αquestion
␈↓ ↓H␈↓of␈αequivalence␈αof␈αconditional␈αforms␈αis␈αdecidable␈αby␈αtruth␈αtables␈αanalogously␈αto␈αthe␈αdecidability␈αof
␈↓ ↓H␈↓Boolean forms.
␈↓ ↓H␈↓3. ␈↓αLambda-expressions.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∞only␈α∞additional␈α∞rule␈α
required␈α∞for␈α∞handling␈α∞lambda-expressions␈α
in␈α∞first␈α∞order␈α∞logic␈α
is
␈↓ ↓H␈↓called ␈↓↓lambda-conversion␈↓, essentially
␈↓ ↓H␈↓␈↓ α_␈↓↓[λx:e][a] =␈↓ <the result of substituting ␈↓↓e␈↓ for ␈↓↓x␈↓ in ␈↓↓a␈↓>.
␈↓ ↓H␈↓As examples of this rule, we have
␈↓ ↓H␈↓␈↓ α_␈↓↓[λx:␈↓αa␈↓↓ x . y][u . v] = [␈↓αa␈↓↓[u . v]] . y␈↓.
␈↓ ↓H␈↓However,␈αa␈α
complication␈αrequires␈α
modifying␈αthe␈α
rule.␈α Namely,␈α
we␈αcan't␈α
substitute␈αfor␈α
a␈αvariable
␈↓ ↓H␈↓and␈α
expression␈αthat␈α
has␈α
a␈αfree␈α
variable␈α
into␈αa␈α
context␈α
in␈αwhich␈α
that␈α
free␈αvariable␈α
is␈αbound.␈α
Thus
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *75
␈↓ ↓H␈↓it␈α∂would␈α∂be␈α∂wrong␈α∂to␈α∂substitute␈α∂␈↓↓x + y␈↓␈α∂for␈α∂␈↓↓x␈↓␈α∂in␈α∂␈↓↓∀y:[x + y = z]␈↓␈α∂or␈α∂into␈α∂the␈α⊂term␈α∂␈↓↓[λy:x + y][u + v]␈↓.
␈↓ ↓H␈↓Before␈α∀doing␈α∀the␈α∪substitution,␈α∀the␈α∀variable␈α∪␈↓↓y␈↓␈α∀would␈α∀have␈α∪to␈α∀be␈α∀replaced␈α∪in␈α∀all␈α∀its␈α∪bound
␈↓ ↓H␈↓occurrences by a fresh variable.
␈↓ ↓H␈↓␈↓ α_Lambda-expressions␈α∀can␈α∀always␈α∃be␈α∀eliminated␈α∀from␈α∃sentences␈α∀and␈α∀terms␈α∃by␈α∀lambda-
␈↓ ↓H␈↓conversion,␈αbut␈αthe␈αexpression␈αmay␈α
increase␈αgreatly␈αin␈αlength␈αif␈α
a␈αlengthy␈αterm␈αreplaces␈αa␈α
variable
␈↓ ↓H␈↓that␈α∞occurs␈α∞more␈α∞than␈α∞once␈α∞in␈α
␈↓↓e␈↓.␈α∞ It␈α∞is␈α∞easy␈α∞to␈α∞make␈α
an␈α∞expression␈α∞of␈α∞length␈α∞␈↓↓n␈↓␈α∞whose␈α∞length␈α
is
␈↓ ↓H␈↓increased to 2␈↓¬n␈↓ by converting its ␈↓↓n␈↓ nested lambda-expressions.
␈↓ ↓H␈↓4. ␈↓αAlgebraic axioms for S-expressions and lists.␈↓
␈↓ ↓H␈↓␈↓ α_The␈α∂algebraic␈α∞facts␈α∂about␈α∞S-expressions␈α∂are␈α∞expressed␈α∂by␈α∞the␈α∂following␈α∞sentences␈α∂of␈α∞first
␈↓ ↓H␈↓order logic:
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x.(issexp x ⊃ ␈↓αat␈↓↓ x ∨ (issexp ␈↓αa␈↓↓ x ∧ issexp ␈↓αd␈↓↓ x ∧ x = (␈↓αa␈↓↓ x . ␈↓αd␈↓↓ x)))␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x y.(issexp x ∧ issexp y ⊃ issexp(x.y) ∧ ¬␈↓αat␈↓↓(x.y) ∧ x = ␈↓αa␈↓↓(x.y) ∧ y = ␈↓αd␈↓↓(x.y))␈↓.
␈↓ ↓H␈↓Here␈α␈↓↓issexp␈αe␈↓␈αasserts␈αthat␈αthe␈αobject␈α␈↓↓e␈↓␈αis␈αan␈αS-expression␈αso␈αthat␈αthe␈αsentences␈αused␈αin␈αproving␈αa
␈↓ ↓H␈↓particular␈α
program␈αcorrect␈α
can␈αinvolve␈α
other␈αkinds␈α
of␈α
entities␈αas␈α
well.␈α If␈α
we␈αcan␈α
assume␈α
that␈αall
␈↓ ↓H␈↓objects␈αare␈αS-expressions␈αor␈αcan␈αdeclare␈αcertain␈αvariables␈αas␈αranging␈αonly␈αover␈αS-expressions,␈αwe
␈↓ ↓H␈↓can simplify the axioms to
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x.[␈↓αat␈↓↓ x ∨ x = [␈↓αa␈↓↓ x . ␈↓αd␈↓↓ x]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x y.[¬␈↓αat␈↓↓[x.y] ∧ x = ␈↓αa␈↓↓[x.y] ∧ y = ␈↓αd␈↓↓[x.y]]␈↓.
␈↓ ↓H␈↓␈↓ α_The algebraic facts about lists are expressed by the following sentences of first order logic:
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x. islist x ⊃ x = NIL ∨ islist ␈↓αd ␈↓↓x␈↓,
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x y. islist y ⊃ islist[x . y]␈↓,
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x y. islist y ⊃ ␈↓αa␈↓↓[x . y] = x ∧ ␈↓αd␈↓↓[x.y] = y␈↓.
␈↓ ↓H␈↓We␈αcan␈α
rarely␈αassume␈α
that␈αeverything␈α
is␈αa␈αlist,␈α
because␈αthe␈α
lists␈αusually␈α
contain␈αatoms␈α
which␈αare
␈↓ ↓H␈↓not themselves lists.
␈↓ ↓H␈↓␈↓ α_These␈α
axioms␈α
are␈α
analogous␈α
to␈α
the␈αalgebraic␈α
part␈α
of␈α
Peano's␈α
axioms␈α
for␈α
the␈αnon-negative
␈↓ ↓H␈↓integers.␈α The␈αanalogy␈αcan␈αbe␈αmade␈αclear␈αif␈αwe␈αwrite␈αPeano's␈αaxioms␈αusing␈α␈↓↓n'␈↓␈αfor␈αthe␈αsuccessor␈αof
␈↓ ↓H␈↓␈↓↓n␈↓ and ␈↓↓n␈↓¬-␈↓ for the predecessor of ␈↓↓n␈↓. Peano's algebraic axioms then become
␈↓ ↓H␈↓␈↓ α_␈↓↓∀n: n' ≠ 0␈↓,
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *76
␈↓ ↓H␈↓␈↓ α_␈↓↓∀n: (n')␈↓¬-␈↓↓ = n␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α_␈↓↓∀n: n ≠ 0 ⊃ (n␈↓¬-␈↓↓)' = n␈↓.
␈↓ ↓H␈↓Integers␈αspecialize␈α lists␈αif␈αwe␈αidentify␈α0␈αwith␈αNIL␈α
and␈αassume␈αthat␈αthere␈αis␈αonly␈αone␈αobject␈α(say␈α
1)
␈↓ ↓H␈↓that can serve as a list element. Then ␈↓↓n' = cons[1,n]␈↓, and ␈↓↓n␈↓¬-␈↓↓ = ␈↓αd␈↓↓ n␈↓.
␈↓ ↓H␈↓␈↓ α_Clearly␈α∞S-expressions␈α
and␈α∞lists␈α
satisfy␈α∞the␈α∞axioms␈α
given␈α∞for␈α
them,␈α∞but␈α∞unfortunately␈α
these
␈↓ ↓H␈↓algebraic␈α∞axioms␈α∞are␈α∞insufficient␈α∞to␈α∞characterize␈α∂them.␈α∞ For␈α∞example,␈α∞consider␈α∞a␈α∞domain␈α∂of␈α∞one
␈↓ ↓H␈↓element a satisfying
␈↓ ↓H␈↓␈↓ α_␈↓αa␈↓↓ a = ␈↓αd␈↓↓ a = a . a = a␈↓.
␈↓ ↓H␈↓It␈αsatisfies␈αthe␈αalgebraic␈αaxioms␈αfor␈αS-expressions.␈α We␈αcan␈αexclude␈αit␈αby␈αan␈αaxiom␈α␈↓↓∀x.(␈↓αa␈↓↓␈αx␈α≠␈αx)␈↓,
␈↓ ↓H␈↓but␈αthis␈αwon't␈α
exclude␈αother␈αcircular␈αlist␈α
structures␈αthat␈αeventually␈αreturn␈α
to␈αthe␈αsame␈α
element␈αby
␈↓ ↓H␈↓some␈α∂␈↓αa-d␈↓␈α∂chain.␈α∂ Actually␈α∞we␈α∂want␈α∂to␈α∂exclude␈α∂all␈α∞infinite␈α∂chains,␈α∂because␈α∂most␈α∂LISP␈α∞programs
␈↓ ↓H␈↓won't␈αterminate␈αunless␈αevery␈α␈↓αa-d␈↓␈αchain␈αeventually␈αterminates␈αin␈αan␈αatom.␈α This␈αcannot␈αbe␈αdone␈αby
␈↓ ↓H␈↓any finite set of axioms.
␈↓ ↓H␈↓5. ␈↓αAxiom schemas of induction.␈↓
␈↓ ↓H␈↓␈↓ α_In␈αorder␈αto␈α
exclude␈αinfinite␈αlist␈αstructures␈α
we␈αneed␈αaxiom␈α
schemas␈αof␈αinduction␈αanalogous␈α
to
␈↓ ↓H␈↓Peano's for the integers. Peano's induction schema is ordinarily written
␈↓ ↓H␈↓␈↓ α_␈↓↓P(0) ∧ ∀n:(P(n) ⊃ P(n')) ⊃ ∀n:P(n)␈↓.
␈↓ ↓H␈↓Here␈α␈↓↓P(n)␈↓␈αis␈αan␈αarbitrary␈α
predicate␈αof␈αintegers,␈αand␈αwe␈α
get␈αparticular␈αinstances␈αof␈αthe␈α
schema␈αby
␈↓ ↓H␈↓substituting␈αparticular␈αpredicates.␈α It␈αis␈αcalled␈αan␈αaxiom␈αschema␈αrather␈αthan␈αan␈αaxiom,␈αbecause␈αwe
␈↓ ↓H␈↓consider␈α∂the␈α∂schema,␈α∂which␈α∂is␈α∂not␈α∂properly␈α⊂a␈α∂sentence␈α∂of␈α∂first␈α∂order␈α∂logic,␈α∂as␈α∂standing␈α⊂for␈α∂the
␈↓ ↓H␈↓infinite collection of axioms that arise from it by substituting all possible predicates for ␈↓↓P␈↓.
␈↓ ↓H␈↓Peano's induction schema can also be written
␈↓ ↓H␈↓␈↓ α_␈↓↓∀n:(n = 0 ∨ P(n␈↓¬-␈↓↓) ⊃ P(n)) ⊃ ∀n:P(n)␈↓,
␈↓ ↓H␈↓and the equivalence of the two forms is easily proved.
␈↓ ↓H␈↓␈↓ α_The S-expression analog is
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x:[issexp x ⊃ [␈↓αat␈↓↓ x ∨ P[␈↓αa␈↓↓ x] ∧ P[␈↓αd␈↓↓ x] ⊃ P[x]]] ⊃ ∀x:[issexp x ⊃ P[x]]␈↓,
␈↓ ↓H␈↓or, assuming everything is an S-expression
␈↓ ↓H␈↓␈↓ α_␈↓↓∀x:[␈↓αat␈↓↓ x ∨ P[␈↓αa␈↓↓ x] ∧ P[␈↓αd␈↓↓ x] ⊃ P[x]] ⊃ ∀x:P[x]␈↓.
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *77
␈↓ ↓H␈↓␈↓ α_The corresponding axiom schema for lists is
␈↓ ↓H␈↓␈↓ α_␈↓↓∀u:[islist u ⊃ [␈↓αn␈↓↓ u ∨ P[␈↓αd␈↓↓ u] ⊃ P[u]]] ⊃ ∀u:[islist u ⊃ P[u]]␈↓.
␈↓ ↓H␈↓␈↓ α_These␈α∂schemas␈α∞are␈α∂called␈α∞principles␈α∂of␈α∞␈↓↓structural␈α∂induction␈↓,␈α∞since␈α∂the␈α∞induction␈α∂is␈α∂on␈α∞the
␈↓ ↓H␈↓structure of the entities involved.
␈↓ ↓H␈↓6. ␈↓αProofs by structural induction.␈↓
␈↓ ↓H␈↓␈↓ α_Recall that the operation of appending two lists is defined by
␈↓ ↓H␈↓1)␈↓ α8{ ␈↓↓u * v ← ␈↓αif n␈↓↓ u ␈↓αthen␈↓↓ v ␈↓αelse a␈↓↓ u . [␈↓αd␈↓↓ u * v]␈↓.
␈↓ ↓H␈↓Let␈α∩us␈α⊃assume␈α∩that␈α∩␈↓↓u␈α⊃*␈α∩v␈↓␈α⊃is␈α∩defined␈α∩for␈α⊃all␈α∩␈↓↓u␈↓␈α⊃and␈α∩␈↓↓v␈↓,␈α∩i.e.␈α⊃the␈α∩computation␈α∩described␈α⊃above
␈↓ ↓H␈↓terminates␈αfor␈αall␈α␈↓↓u␈↓␈αand␈α␈↓↓v␈↓;␈αwe␈αwill␈αshow␈αhow␈αto␈αprove␈αit␈αlater.␈α Then␈αwe␈αcan␈αreplace␈α({[5]␈αsi2})␈αby
␈↓ ↓H␈↓the sentence
␈↓ ↓H␈↓2)␈↓ α8{ ␈↓↓∀u v:[islist u ∧ islist v ⊃ [u * v = ␈↓αif n␈↓↓ u ␈↓αthen␈↓↓ v ␈↓αelse a␈↓↓ u . [␈↓αd␈↓↓ u * v]]]␈↓.
␈↓ ↓H␈↓Now␈α⊂suppose␈α⊂we␈α⊂would␈α⊃like␈α⊂to␈α⊂prove␈α⊂␈↓↓∀v:[NIL␈α⊃*␈α⊂v␈α⊂=␈α⊂v]␈↓.␈α⊂ This␈α⊃is␈α⊂quite␈α⊂trivial;␈α⊂we␈α⊃need␈α⊂only
␈↓ ↓H␈↓substitute NIL for ␈↓↓x␈↓ in ({[5] si1}), getting
␈↓ ↓H␈↓→NIL␈↓↓ * v ␈↓ β( = ␈↓αif n NIL then␈↓↓ v ␈↓αelse a NIL␈↓↓ . [␈↓αd NIL␈↓↓ * v]
␈↓ ↓H␈↓↓␈↓ β( = v␈↓.
␈↓ ↓H␈↓Next␈αconsider␈αproving␈α␈↓↓∀u:[u␈α*␈αNIL␈α=␈αu]␈↓.␈α This␈αcannot␈αbe␈αdone␈αby␈αsimple␈αsubstitution,␈αbut␈αit␈αcan
␈↓ ↓H␈↓be done as follows: First substitute ␈↓↓λu:[u * NIL = u]␈↓ for ␈↓↓P␈↓ in the induction schema
␈↓ ↓H␈↓␈↓ α_␈↓↓∀u:[islist u ⊃ [␈↓αn␈↓↓ u ∨ P[␈↓αd␈↓↓ u] ⊃ P[u]]] ⊃ ∀u:[islist u ⊃ P[u]]␈↓,
␈↓ ↓H␈↓getting
␈↓ ↓H␈↓␈↓ α_␈↓↓∀u:[islist␈αu␈α⊃␈α[␈↓αn␈↓↓␈αu␈α∨␈αλu:[u␈α*␈αNIL␈α=␈αu][␈↓αd␈↓↓␈αu]␈α⊃␈αλu:[u␈α*␈αNIL␈α=␈αu][u]]]␈α⊃␈α∀u:[islist␈αu␈α⊃␈αλu:[u␈α*
␈↓ ↓H␈↓↓NIL = u][u]]␈↓.
␈↓ ↓H␈↓Carrying out the indicated lambda conversions makes this
␈↓ ↓H␈↓3)␈↓ α8{ ␈↓↓∀u:[islist u ⊃ [␈↓αn␈↓↓ u ∨ ␈↓αd␈↓↓ u * ␈↓NIL␈↓↓ = ␈↓αd␈↓↓ u] ⊃ u * ␈↓NIL␈↓↓ = u] ⊃ ∀u:[islist u ⊃ u * ␈↓NIL␈↓↓ = u]␈↓.
␈↓ ↓H␈↓␈↓ α_Next␈α
we␈α
must␈α
use␈α
the␈α
recursive␈α
definition␈α
of␈α
␈↓↓u*v␈↓.␈α
There␈α
are␈α
two␈α
cases␈α
according␈αto␈α
whether
␈↓ ↓H␈↓␈↓αn␈↓␈α␈↓↓u␈↓␈αor␈αnot.␈α In␈αthe␈αfirst␈αcase,␈αwe␈αsubstitute␈αNIL␈αfor␈α␈↓↓v␈↓␈αand␈αget␈αNIL*NIL = NIL,␈αand␈αin␈αthe␈αsecond
␈↓ ↓H␈↓case␈α
we␈α
use␈α
the␈α
hypothesis␈α
␈↓αd␈↓ u * NIL = ␈↓αd␈↓ u␈α
and␈α∞the␈α
third␈α
algebraic␈α
axiom␈α
for␈α
lists␈α
to␈α∞make␈α
the
␈↓ ↓H␈↓simplification
␈↓ ↓H␈↓4)␈↓ α8{ ␈↓↓␈↓αa␈↓↓ u . [␈↓αd␈↓↓ u * ␈↓NIL␈↓↓] = ␈↓αa␈↓↓ u . ␈↓αd␈↓↓ u = u.␈↓
␈↓ ↓H␈↓␈↓ ¬hCHAPTER V␈↓ *78
␈↓ ↓H␈↓Combining␈α⊂the␈α⊂cases␈α∂gives␈α⊂the␈α⊂hypothesis␈α∂of␈α⊂({eq␈α⊂si3})␈α∂and␈α⊂hence␈α⊂its␈α∂conclusion,␈α⊂which␈α⊂is␈α∂the
␈↓ ↓H␈↓statement to be proved.
␈↓ ↓H␈↓7. ␈↓αFirst Order Theory of Partial Functions␈↓